如何在一行中获取具有相同id但在另一列中具有不同值的数据并在php中显示到表中

时间:2017-01-04 08:02:39

标签: php mysql

我在SQL表中有以下数据:

id      code     day     time    year
 1     PRC-001    0       t-1     2017
 2     PRC-001    1       t-2     2017
 3     PRC-002    0       t-3     2017
 4     PRC-002    1       t-4     2017
 5     PRC-003    0       t-5     2017
 6     PRC-003    1       t-6     2017

输出应该是这样的:

day0     day1     code      
 t-1      t-2    PRC-001
 t-3      t-4    PRC-002    
 t-5      t-6    PRC-003   

我该怎么做?我正在尝试类似下面的代码。但我没有得到任何欲望输出。这是我的代码:

$query1 = "SELECT * FROM routine AS tab1,"; 
$query1.= " GROUP BY code";

$rs = mysql_query($query1);
$numOfRows=mysql_num_rows($rs);
$printed = array();
$resultset = array();

while($row = mysql_fetch_assoc($rs)) {
    $resultset[] = $row;
    #print_r($row);
}

$q = "<table id='ash'>";
$q.= "<tr id='grey'>";
$q.= "<th rowspan='2'>day0</th>";
$q.= "<th rowspan='2'>day1(s)</th>";
$q.= "<th rowspan='2'>code</th></tr>";
$q.= "</tr>";


foreach ($resultset as $row){ 
    $q.= "<tr>";
    $q.= "<tr><td id='clist'>".$row["time"]."</td>";
    $q.= "<td id='clist'>".$row["time"]."</td>";
    $q.= "<td id='clist'>".$row["code"]."</td></tr>";
} 
$q .= "</table>";
echo $q;

2 个答案:

答案 0 :(得分:1)

首先,正如评论中所提到的,您应该考虑使用mysql_函数之外的其他内容。

其次,在您查询时,您需要删除GROUP BY

然后你可以这样做:

$rs = mysql_query("SELECT * FROM routine");

$results = [];

while ($row = mysql_fetch_assoc($rs)) {

    $code = $row['code'];

    if (!isset($results[$code])) {
        $results[$code] = [
            'day0' => '-',
            'day1' => '-',
        ];
    }

    $results[$code]['day' . $row['day']] = $row['time'];

}

?>

<table>
    <thead>
    <tr id="grey">
        <th rowspan="2">Day0</th>
        <th rowspan="2">Day1(s)</th>
        <th rowspan="2">code</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($results as $code => $result) : ?>
        <!--You shouldn't have multiple elements using the same ids-->
        <tr>
            <td id='clist'><?php echo $result['day0'] ?></td>
            <td id='clist'><?php echo $result['day1'] ?></td>
            <td id='clist'><?php echo $code ?></td>
        </tr>
    <?php endforeach ?>
    </tbody>
</table>

希望这有帮助!

答案 1 :(得分:0)

根据您所需的表,行中的列不在数据库表的一行中!所以你不能通过查询结果只使用foreach构建表。

例如,

t-1, t-2 and PRC-001在数据库中不在一行中。如果day0是t-1,那么day1将为空,反之亦然。

<强>溶液

你必须在empty or zero中将days放在决赛桌中才有意义,而且你不需要groupby

$query1 = "SELECT * FROM routine AS tab1"; 


$rs = mysql_query($query1);
$numOfRows=mysql_num_rows($rs);
$printed = array();
$resultset = array();

while($row = mysql_fetch_assoc($rs)) {
    $resultset[] = $row;
    #print_r($row);
}

$q = "<table id='ash'>";
$q.= "<tr id='grey'>";
$q.= "<th rowspan='2'>day0</th>";
$q.= "<th rowspan='2'>day1(s)</th>";
$q.= "<th rowspan='2'>code</th></tr>";
$q.= "</tr>";


foreach ($resultset as $row){ 

    if($row['day'] == 0){
        $q.= "<tr>";
        $q.= "<tr><td id='clist'>".$row["time"]."</td>";
        $q.= "<td id='clist'>"EMPTY!"</td>";
        $q.= "<td id='clist'>".$row["code"]."</td></tr>";
    } else {
        $q.= "<tr>";
        $q.= "<tr><td id='clist'>"EMPTY!"</td>";
        $q.= "<td id='clist'>".$row["time"]."</td>";
        $q.= "<td id='clist'>".$row["code"]."</td></tr>";   
    }

} 
$q .= "</table>";
echo $q;