douqian8238 2018-06-20 02:08
浏览 67
已采纳

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

I have searched this for hours and read numerous q/a's on foreach loops and while loops for the answer to my question but have yet to find a response that resembled my query. Mostly categorized menues...

I have a mysql table setup like this

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 

and would like to group the data in a php looped table like this;

         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 

What sort of nested loop would I need? would I need to call a second mysqli query in the nested loop grouping rows by cat ?

Thanks :)

PHP code added Edit

$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>";
  • 写回答

2条回答 默认 最新

  • dswe30290 2018-06-20 04:29
    关注

    Mostly it will be like the below.

    The idea is

    1. Sort the rows with category. [you have done this part.]
    2. Declare the category with a default value.
    3. If there is any change, update the category value with new one. [as it is sorted, it will always grouped by default. ]

    The code will be like this.

    <?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>";
    

    Check on. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题