在HTML中显示mysql表,合并第二列中具有相同值的所有行


Display mysql table in HTML, merge all rows that has same values in second column

我有下面的php代码,它将mysql表显示为html表。Mysql 表包含 4 列:日期、ID、项目、数量。

在 html 表中显示时,我想合并第二列中具有相同"id"的所有行。

任何帮助非常感谢。

$sql = "SELECT * FROM table_name";
$result = mysqli_query($db, $sql);
if ($result==True) 
{   
    echo "<table border='3' style='position:absolute;width:60%;top:20px;left:20%;text-align:center;border-collapse: collapse;'>";
    echo "<tr>";
        echo "<th>Date</th>";
        echo "<th>id</th>";
        echo "<th>item</th>";
        echo "<th>quantity</th>";       
    echo "</tr>";
    while($row = mysqli_fetch_row($result))
    {
        echo "<tr>";            
            echo "<td>$row[1]</td>";
            echo "<td>$row[2]</td>";
            echo "<td>$row[3]</td>";
            echo "<td>$row[4]</td>";            
        echo "</tr>";
    }
    mysqli_close($db);
} 

我在 Mysql 中的表如下所示:

    Date         Id     Item    Quantity
    1/1/2015    12345   Apple   1 KG
    1/1/2015    12345   Apple   1 KG
    1/1/2015    12345   Apple   1 KG
    1/1/2015    97786   Apple   1 KG
    1/1/2015    76767   Apple   1 KG
    1/1/2015    24335   Apple   1 KG
    1/1/2015    9098    Apple   1 KG

我希望我的 html 表格如下所示:(第 1、2 和 3 行合并,因为它具有相同的 ID(

    Date         Id     Item    Quantity
    1/1/2015            Apple   1 KG
    1/1/2015    12345   Carrot  1 KG
    1/1/2015            Onion   1 KG
    1/1/2015    97786   Apple   1 KG
    1/1/2015    76767   Apple   1 KG
    1/1/2015    24335   Apple   1 KG
    1/1/2015    9098    Apple   1 KG

因为它似乎不是最好的解决方案,但它应该可以工作..尝试

$check=array();
while($row = mysqli_fetch_row($result))
    {
      if(in_array($row[2],$check)
        $row[2]="";
        echo "<tr>";            
            echo "<td>$row[1]</td>";
            echo "<td>$row[2]</td>";
            echo "<td>$row[3]</td>";
            echo "<td>$row[4]</td>";            
        echo "</tr>";
        $check[]=$row[2];
    }

如果您不介意多循环一次(和一点(,则可以使用rowspan来完成。

$table = array();
while($row = mysqli_fetch_row($result)) {
    $table[$row[1]][] = array($row[0],$row[2],$row[3]);
}
echo "<table border='3' style='position:absolute;width:60%;top:20px;left:20%;text-align:center;border-collapse: collapse;'>";
echo "<thead>";
    echo "<tr>";
        echo "<th>Date</th>";
        echo "<th>id</th>";
        echo "<th>item</th>";
        echo "<th>quantity</th>";
    echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach($table as $id=>$idRows) {
    $rowspan = count($idRows);
    for($i = 0; $i < $rowspan; $i++) {
        echo "<tr>";
            echo "<td>{$idRows[$i][0]}</td>";
            if($i === 0)
                echo "<td rowspan={$rowspan}>{$id}</td>";
            echo "<td>{$idRows[$i][1]}</td>";
            echo "<td>{$idRows[$i][2]}</td>";
        echo "</tr>";
    }
}
echo "</tbody>";
echo "</table>";
I have updated my answer. Please see it and try

//Set all value in some temp array 
$array_date = "";
$array_id = "";
$array_item = "";
$array_qty = "";
$array_rowspan = "";
$i=0;
while($row = mysqli_fetch_row($result))
{

    if($tempid != $row[1]){
        $tempid = $row[1];
        $rowspan = 1;       
        $array_date[] = $row[0]; 
        $array_id[] = $row[1];
        $array_item[] = $row[2];
        $array_qty[] = $row[3];         
        $array_rowspan[$i] = $rowspan;
        //Set another temp value to set same rowspan in mached category id  
        $same_id=1; 
    }else{
        $rowspan++;     
        $array_date[] = $row[0];
        $array_id[] = $row[1];
        $array_item[] = $row[2];
        $array_qty[] = $row[3];
        //Set another temp value to set same rowspan in mached category id          
        $same_id++;     
        for($j=0; $j<$same_id; $j++){
            $array_rowspan[$i-($j)] = $rowspan;
        }
    }       
    $i++;   
}
echo "<table border='1' style='position:absolute;width:60%;top:20px;left:20%;text-align:center;border-collapse: collapse;'>";
    echo "<tr>";
        echo "<th>Date</th>";
        echo "<th>id</th>";
        echo "<th>item</th>";
        echo "<th>quantity</th>";       
     echo "</tr>";
    //Set a temp variable like for id
    $tempid2 = "";
    foreach($array_id as $key => $val){
        if($tempid2 != $val){
            $tempid2 = $val;
            echo "<tr>"; 
            echo "<td>".$array_date[$key]."</td>";
            echo "<td rowspan='".$array_rowspan[$key]."'>".$array_id[$key]."</td>";     
            echo "<td>".$array_item[$key]."</td>";
            echo "<td>".$array_qty[$key]."</td>"; 
            echo "</tr>";
        }else{
            echo "<tr>"; 
            echo "<td>".$array_date[$key]."</td>";              
            echo "<td>".$array_item[$key]."</td>";
            echo "<td>".$array_qty[$key]."</td>"; 
            echo "</tr>";
        }
    }
 echo "</table>";

请尝试此

将分组依据与列名一起使用

$sql = "SELECT * FROM table_name GROUP BY `id`";