我正在创建一个接收用户订单的表,C_ID将存储在C_ID唯一的(StationaryCustomersInfo)表中,并且表(StationaryOrders)将存储每个具有C_ID的项目,而C_ID将存储在(StationaryOrders)中该表不是唯一的,因为C_ID将与该C_ID所属的每个项目重复。
我想通过C_ID显示每个订单,每个客户的每个订单都将使用单独的表格显示。
$sql=mysqli_query($link, "SELECT Item, Personal, Department, Quantity, Color FROM StationaryOrders, StationaryCustomersInfo WHERE StationaryCustomersInfo.C_ID = StationaryOrders.C_ID");
?>
<table align="center" border='3' style="width:60%; line-height:30px; border-color: #020B58;">
<tr>
<th>Items</th>
<th>Personal</th>
<th>Department</th>
<th>Quantity</th>
<th>Color</th>
</tr>
<?php
if($sql){
while($row=mysqli_fetch_assoc($sql)){
?>
<tr>
<td style="text-align:center"><?php echo $row['Item']; ?></td>
<td style="text-align:center"><?php echo $row['Personal']; ?></td>
<td style="text-align:center"><?php echo $row['Department']; ?></td>
<td style="text-align:center"><?php echo $row['Quantity']; ?></td>
<td style="text-align:center"><?php echo $row['Color']; ?></td>
</tr>
<?php
}
}
?>
</table>
我希望用单独的表格显示每个客户的订单,并且按钮属于该表格。
答案 0 :(得分:0)
这可以通过在查询中添加C_ID
并按C_ID
对结果进行排序来完成。然后,在php中,您可以检查以前的抓取C_ID
是否与当前抓取相同。
例如:
// Let's factorise the building of the <table> header there to avoid duplication in code
function ShowTableHeader($cid)
{
?>
<table align="center" border='3' style="width:60%; line-height:30px; border-color: #020B58;">
<caption>Orders of user <?php echo $cid; ?> : </caption>
<tr>
<th>Items</th>
<th>Personal</th>
<th>Department</th>
<th>Quantity</th>
<th>Color</th>
</tr>
<?php
}
$query = "SELECT sci.C_ID, "
. "Item, "
. "Personal, "
. "Department, "
. "Quantity, "
. "Color "
."FROM StationaryOrders so "
."LEFT JOIN StationaryCustomersInfo sci "
."ON sci.C_ID = so.C_ID "
."ORDER BY sci.C_ID";
$sql = mysqli_query($link, $query);
if ($sql)
{
if ($row=mysqli_fetch_assoc($sql))
{
// initialize the first C_ID for the first result
$previousCID = $row['C_ID'];
ShowTableHeader($previousCID);
do
{
// different C_ID than before ? Let's build a new <table>
if ($previousCID != $row['C_ID'])
{
echo "</table>";
$previousCID = $row['C_ID'];
ShowTableHeader($previousCID);
}
?>
<tr>
<td style="text-align:center"><?php echo $row['Item']; ?></td>
<td style="text-align:center"><?php echo $row['Personal']; ?></td>
<td style="text-align:center"><?php echo $row['Department']; ?></td>
<td style="text-align:center"><?php echo $row['Quantity']; ?></td>
<td style="text-align:center"><?php echo $row['Color']; ?></td>
</tr>
<?php
} while ($row=mysqli_fetch_assoc($sql));
echo "</table>";
}
}