2017-06-08 72 views
2

问题1:while循环的输出结果作为第二个while循环的查询变量吗?PHP第一while循环的输出结果为第二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 

请帮助找到我的错误和解决方案。谢谢。

+0

字符串应该用单引号括起来 “......供应商= '$ supplier4'” – JYoThI

+0

我试图针锋相对...... “供应商= '$ supplier4'”,不工作... –

+0

你需要通过每个供应商更改这样的查询“... supplier ='$ row4 ['supplier']'”@p。 Lau – JYoThI

回答

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); 
?> 
+0

是的!达的作品,但.....数字(否)是1,2,3,4。它必须是1,2,1,1。 –

+0

是的,您需要重置每个新供应商的$ i = 1。检查我的更新1 @ P.Lau – JYoThI

+0

我自己解决它oredi!是!!谢谢你们......你救了我的一天,并推动我前进! –

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>"; 
+0

我的表可能会在未来变大,这会比我的双查询循环运行得更快吗? –

+2

是的,这是一个更好的方法 - 因为在这样的另一个查询中运行查询是不好的做法。如果有9个不同的供应商,您最终将得到4 ++查询(如果有1个查询,则可以得到10个查询)。如果可以,应该避免在另一个查询中查询,并且通过简单的调整,就可以轻松避免它,如上所示。 – Qirel

+0

雅作品2!谢啦!我对PHP很陌生,所以我知道的唯一方法是在循环内循环。现在我有更多的选择。我会在表增长时使用它。 –