另一个嵌套循环 php + mysql 查询

another nested loops php + mysql query

我已经搜索了数小时并阅读了大量 q/a 的 foreach 循环和 while 循环以找到我问题的答案,但尚未找到与我的查询相似的响应。主要分类菜单...

我有一个 mysql table 这样的设置

cat | product | link  | status
1   | milk    | https | in stock
1   | eggs    | https | in stock
2   | butter  | https | out of stock
2   | bread   | https | in stock
3   | bananas | https | in stock 

并希望将数据分组到 php 循环 table 中;

         Category 1
 milk    | https | in stock
 eggs    | https | in stock
         Category 2
 butter  | https | out of stock
 bread   | https | in stock
         Category 3
 bananas | https | in stock 

我需要什么样的嵌套循环?我是否需要在按 cat 对行进行分组的嵌套循环中调用第二个 mysqli 查询?

谢谢:)

PHP 代码添加编辑

$stmt = $con->prepare("SELECT * FROM wp_products ORDER BY cat");
$stmt->execute();

$results = $stmt->get_result();
echo "<p><center><h4>Master List</h4></center></p>";
echo "<table>
<tr>
<th>Product</th>
<th>Link</th>
<th>Status</th>
</tr>";
if (mysqli_num_rows($results)>0){
while($row = $results->fetch_assoc())
{
  echo "<tr><td>" . $row['product'] ."</td>";
  echo "<td>". $row['link'] ."</td>;
  echo "<td>". $row['status'] ."</td></tr>";
}
}
echo "</table>";

没有包含任何代码,因为您没有提供任何开始,但恕我直言,这个概念是合理的。

  • Select数据库中最高catSELECT MAX(cat) FROM ...
  • 从 1 循环到最高 cat。在每个循环中,select 与当前循环索引 SELECT cat,product,link,status FROM ... 匹配的条目。
  • 在结果上循环(foreach here with DB results)并构建`

    <table>
      <theader>
        <tr><th colspan="3">Category $loopindex</th><tr>
      </theader>
      <tbody>
        <tr>
          <td>$result[product]</td>
          <td>$result[link]</td>
          <td>$result[status]</td>
        </tr>
      </tbody>
    </table>
    
  • 循环索引增加+1

  • 进入下一个类别

有许多细节需要解决(正确的 PHP/HTML 代码,CSS 用于您的 table,如何使用 PHP 访问数据库,...) ,不过原理还是可以的


好的,你在我回答后添加了代码。

  • SELECT * 的问题是您一次获得整个 table。对于小型数据库可能没问题,但这是您应该避免的习惯。总有一天你会得到大量的数据集(或者这个数据集会增长)。
  • 此外,您还必须循环查看所有结果以及 table 每个结果所在的数字。您必须将其存储在一个数组(?)中,并从这些数组输出您的 html table 代码 ?
  • 您的 ORDER BY 值未列在您的结果中(按名称)。
  • 如果您执行了 ORDER BY cat,至少您会知道每个条目都是将 <table> html 直接输出到页面的正确顺序,而无需在中间.
  • 这样每次看到一个新的 cat 值时,您都可以开始一个新的 table,或者添加一个 <tr><td colspan="3">Category 1</td></tr> 分隔线。

回复询问类别循环的评论。

让我们假设这个 table:

name product link status
 1   p1      l1    s1
 2   p2      l2    s2
 1   p3      l3    s3
 1   p4      l4    s4

查询SELECT cat,product,link,status FROM table ORDER BY cat,product 会给你这个结果集:

1,p1,l1,s1
1,p3,l3,s3
1,p4,l4,s3
2,p2,l2,s2

因此,您可以循环处理这些结果,一次一行。每个循环必须为下一个循环保留 cat 值。如果它发生变化,您可以在此处放置 header 来标识类别。然后继续打印行,直到它们全部完成,或者直到找到新类别。等等。

大部分会像下面这样

想法是

  1. 按类别对行进行排序。 [你已经完成了这部分。]
  2. 使用默认值声明类别。
  3. 如果有任何变化,请用新的类别值更新。 [因为它是排序的,它总是默认分组。 ]

代码是这样的

<?php

$stmt = $con->prepare("SELECT * FROM wp_products ORDER BY cat");
$stmt->execute();

$category = '';  # Default value for category. 

$results = $stmt->get_result();
echo "<p><center><h4>Master List</h4></center></p>";
echo "<table>
<tr>
<th>Product</th>
<th>Link</th>
<th>Status</th>
</tr>";
if (mysqli_num_rows($results) > 0) {
   while ($row = $results->fetch_assoc()) {

      # Category will be updated in this query 
      if($category != $row['cat']) { 
         $category = $row['cat']; 
         echo '<tr><td colspan="3"> Category ' . $category .' </td></tr>'; 
      }

      echo "<tr><td>" . $row['product'] . "</td>";
      echo "<td>" . $row['link'] . "</td>";
      echo "<td>" . $row['status'] . "</td></tr>";
   }
}
echo "</table>";

检查。 :)