问题 1:是否可以将第一个 while 循环的输出结果作为第二个 while 循环的变量进行查询?
我在下面有mySQL表"reorder_in_process":
(`id`, `item`, `to_order`, `cat_no`, `supplier`) VALUES
(48, 'Petri Dish', 62, 1006, 'Progressive'),
(47, 'Beaker', 46, 1005, 'Progressive'),
(46, 'Tissue', 17, 1008, 'Needpoint'),
(45, 'Pipet', 77, 1004, 'Kumpulan');
还有我在下面的php:
<?PHP
include ('db.php');
$sql4 = "SELECT DISTINCT supplier FROM reorder_in_process";
$result4 = mysqli_query($con,$sql4);
$supplier4 = $row4['supplier'];
$i=0;
while($row4 = mysqli_fetch_array($result4)){
echo "<h4>".$row4['supplier']."</h4>";
echo "<div><table><tr>
<th>No</th>
<th>Item</th>
<th>Cat. No</th>
<th>Buy QTY</th>
<th>Supplier</th>
</tr>";
$sql5 = "SELECT * FROM reorder_in_process WHERE supplier=$supplier4";
$result5 = mysqli_query($con,$sql5);
while($row5 = mysqli_fetch_array($result5)) {
$i++;
echo "<tr>
<td>".$i. "</td>
<td>".$row5['item']."</td>
<td>".$row5['cat_no']."</td>
<td>".$row5['to_order']."</td>
<td>".$row5['supplier']."</td>
</tr>";
}
echo "</table></div>";
}
mysqli_close($con);
?>
我不工作...我想要如下所示的输出结果:
Progressive
No Item Cat. No Buy QTY Supplier
1 Petri Dish 1006 62 Progressive
2 Beaker 1005 46 Progressive
Needpoint
No Item Cat. No Buy QTY Supplier
1 Tissue 1008 17 Needpoint
Kumpulan
No Item Cat. No Buy QTY Supplier
1 Pipet 1004 77 Kumpulan
请帮助找到我的错误并解决它。谢谢。
你不需要像这样运行两个查询。
不要执行一个查询,只是为了在循环中再次查询同一个表,请修改结构,以便只查询一次表。只需ORDER BY supplier
并检查当前行供应商是否与最后一行供应商不同。(披露:我还没有完全测试这个逻辑,可能需要一些调整(。
$result = mysqli_query($con, "SELECT * FROM reorder_in_process ORDER BY supplier");
$first_iteration = true;
$current_supplier = null;
while($row = mysqli_fetch_assoc($result)){
// If a new supplier is encountered, close the first table (if its no the first iteration) and add new header + start new table
if ($row['supplier'] != $current_supplier) {
if ($first_iteration == false)
echo "</table></div>";
$i = 1;
$first_iteration = false;
$current_supplier = $row['supplier'];
echo "<h4>".$row['supplier']."</h4>";
echo "<div><table><tr>
<th>No</th>
<th>Item</th>
<th>Cat. No</th>
<th>Buy QTY</th>
<th>Supplier</th>
</tr>";
}
echo "<tr>
<td>".$i. "</td>
<td>".$row['item']."</td>
<td>".$row['cat_no']."</td>
<td>".$row['to_order']."</td>
<td>".$row['supplier']."</td>
</tr>";
$i++;
}
echo "</table></div>";
首先,下面的行是未定义的,因为它在 while 循环之外
$supplier4 = $row4['supplier'];
首先将上面的代码推入内部,而循环和字符串应该像这样用单引号括起来
"...supplier='$supplier4'"
您需要为每个新供应商重置$i=1
更新 1:
<?PHP
include ('db.php');
$sql4 = "SELECT DISTINCT supplier FROM reorder_in_process";
$result4 = mysqli_query($con,$sql4);
while($row4 = mysqli_fetch_array($result4)){
$i=1;
$supplier4 = $row4['supplier'];
echo "<h4>".$row4['supplier']."</h4>";
echo "<div><table><tr>
<th>No</th>
<th>Item</th>
<th>Cat. No</th>
<th>Buy QTY</th>
<th>Supplier</th>
</tr>";
$sql5 = "SELECT * FROM reorder_in_process WHERE supplier='$supplier4'";
$result5 = mysqli_query($con,$sql5);
while($row5 = mysqli_fetch_array($result5)) {
echo "<tr>
<td>".$i. "</td>
<td>".$row5['item']."</td>
<td>".$row5['cat_no']."</td>
<td>".$row5['to_order']."</td>
<td>".$row5['supplier']."</td>
</tr>";
$i++;
}
echo "</table></div>";
}
mysqli_close($con);
?>