我有以下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
答案 0 :(得分:0)
因为它似乎不是最佳解决方案,但它应该起作用..
$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];
}
答案 1 :(得分:0)
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>";
请试试这个
答案 2 :(得分:0)
如果您不介意在数据中循环一段额外的时间(以及一点点),您可以使用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>";
答案 3 :(得分:-2)
使用group by with column name
$sql = "SELECT * FROM table_name GROUP BY `id`";