问题1:是否可以将第一次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
请帮助找出我的错误和解决方案。感谢。
答案 0 :(得分:1)
您不需要像这样运行两个查询。
不是执行一个查询,而只是在循环内再次查询相同的表,修改您的结构,这样您只需查询一次表。只需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>";
答案 1 :(得分:0)
首先,下面的行是未定义的,因为它在while循环之外
$supplier4 = $row4['supplier'];
将上面的代码推入内部,while循环和字符串应该用这样的单引号括起来
"...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);
?>