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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动