php:如何将字符串数据更改为数字数据

发布于 2024-09-14 15:52:38 字数 1023 浏览 4 评论 0原文

你能告诉我如何在 php 和 mysql 脚本中更改这个结果吗:

  Model                  Class
Ball                        S
Book                        A
Spoon
Plate                       B
Box                         C

这是我的数据库:

CREATE TABLE IF NOT EXISTS `inspection_report` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Model` varchar(14) NOT NULL,
  `Serial_number` varchar(8) NOT NULL,
  `Lot_no` varchar(6) NOT NULL,
  `Line` char(5) NOT NULL,      
  `Class` char(1) NOT NULL,
  `Status` varchar(6) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `Model` (`Model`,`Serial_number`,`Lot_no`,`Line`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=48 ;

如果我想显示如下结果:

 Model           s       a       b       c
 Ball            1       0       0       0
 Book            0       1       0       0
 Spoon           0       0       0       0
 Plate           0       0       1       0
 Box             0       0       0       1

什么查询可以做到这一点?谢谢。

can you tell how to change this result in php and mysql script:

  Model                  Class
Ball                        S
Book                        A
Spoon
Plate                       B
Box                         C

this is my DB:

CREATE TABLE IF NOT EXISTS `inspection_report` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `Model` varchar(14) NOT NULL,
  `Serial_number` varchar(8) NOT NULL,
  `Lot_no` varchar(6) NOT NULL,
  `Line` char(5) NOT NULL,      
  `Class` char(1) NOT NULL,
  `Status` varchar(6) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `Model` (`Model`,`Serial_number`,`Lot_no`,`Line`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=48 ;

how if i want to show result like:

 Model           s       a       b       c
 Ball            1       0       0       0
 Book            0       1       0       0
 Spoon           0       0       0       0
 Plate           0       0       1       0
 Box             0       0       0       1

What's query to make this? thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑梦风尘 2024-09-21 15:52:38
SELECT `Model`,
IF(`Class`='S', 1, 0) AS `S`,
IF(`Class`='A', 1, 0) AS `A`,
IF(`Class`='B', 1, 0) AS `B`,
IF(`Class`='C', 1, 0) AS `C`
FROM `inspection_report`
SELECT `Model`,
IF(`Class`='S', 1, 0) AS `S`,
IF(`Class`='A', 1, 0) AS `A`,
IF(`Class`='B', 1, 0) AS `B`,
IF(`Class`='C', 1, 0) AS `C`
FROM `inspection_report`
青衫负雪 2024-09-21 15:52:38

你的问题有点不清楚,但我假设你有一个数组中的输入数据映射名称到缺陷,并且你希望每行在适当的列中为 1,在其他地方为 0。如果是这样,那就只是这样:

$arr = array('blue' => 'S', 'red' => 'A', 'yellow' => null, 'green' => 'B', 'black' => 'C');

$defects = array_filter(array_unique(array_values($arr)));
echo "name\t";
echo implode("\t", $defects);
echo "\n";

foreach($arr as $name => $defect) {
    echo "$name";
    foreach($defects as $test) {
        echo "\t";
        echo $test == $defect ? 1 : 0;
    }
    echo "\n";
}

Your question is a little unclear, but I'm assuming you have the input data in an array mapping name to defect, and you want for each row a 1 in the appropriate column and a zero everywhere else. If so, it's just this:

$arr = array('blue' => 'S', 'red' => 'A', 'yellow' => null, 'green' => 'B', 'black' => 'C');

$defects = array_filter(array_unique(array_values($arr)));
echo "name\t";
echo implode("\t", $defects);
echo "\n";

foreach($arr as $name => $defect) {
    echo "$name";
    foreach($defects as $test) {
        echo "\t";
        echo $test == $defect ? 1 : 0;
    }
    echo "\n";
}
戏舞 2024-09-21 15:52:38

非常粗略的例子,实际上您可能会使用 HTML 表格。

<?php // $rows = array(array('name' => 'blue', 'class_defect' => 'S'), ...); ?>

<pre>
name      s  a  b  c
<?php
foreach ($rows as $row) {
    printf('%-10s', $row['name']);  // padding with spaces
    foreach (array('s', 'a', 'b', 'c') as $col) {
        echo (strtolower($row['class_defect']) == $col) ? 1 : 0;
        echo '  ';  // just padding again
    }
}
?>
</pre>

Very crude example, you'd probably go with HTML tables in reality.

<?php // $rows = array(array('name' => 'blue', 'class_defect' => 'S'), ...); ?>

<pre>
name      s  a  b  c
<?php
foreach ($rows as $row) {
    printf('%-10s', $row['name']);  // padding with spaces
    foreach (array('s', 'a', 'b', 'c') as $col) {
        echo (strtolower($row['class_defect']) == $col) ? 1 : 0;
        echo '  ';  // just padding again
    }
}
?>
</pre>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文