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 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥30 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图
  • ¥20 simulink实现滑模控制和pid控制对比,提现前者优势