如何将sql循环查询拆分为

时间:2017-02-25 10:40:21

标签: php mysql while-loop

我有一个数据库表haccp,其中包含按criteria分类的sections行。 我正在尝试创建一个查询来获取所有条件并在每个部分下回显它。例如:

第1节

  1. 标准1
  2. 标准2
  3. 第2节

    1. 标准1
    2. 标准2
    3. 我尝试使用WHERE子句编写8个单独的sql查询,并使用while循环来回显每个部分下的结果,但这需要30秒才能运行并加载页面。

      以前的SQL QUERY

      $get_delivery_criteria = $mysqli->prepare("SELECT criteria, section, hazard FROM haccp WHERE section='Delivery' ORDER BY criteria");
      $get_delivery_criteria->execute();
      $get_delivery_criteria->bind_result($criteria_db, $section, $hazard);
      $get_delivery_criteria->store_result();
      $row_cnt = $get_delivery_criteria->num_rows();
      if($row_cnt >= 1)
      {
          while ($get_delivery_criteria->fetch() )
          { 
          ?>
              <li><a href="?criteria=<? echo $criteria_db; ?>"><? echo $criteria_db. " ". $hazard; ?></a></li>    
          <? 
          }
          $get_delivery_criteria->close();
      }
      

      所以我正在开发一个新的查询,列出array中的部分,然后使用foreach循环标准并在标题下显示每个部分。

      新SQL查询

      $sections = array("1.Delivery", "2.Storage", "3.Preparation", "4.Cooking", "5.Serving", "6.Cleaning", "7.Building", "8.Management");
      $get_criteria = $mysqli->prepare("SELECT criteria, section, hazard FROM haccp ORDER BY criteria");
      $get_criteria->execute();
      $get_criteria->bind_result($criteria_db, $section_db, $hazard_db);
      $get_criteria->store_result();                              
      $row_cnt = $get_criteria->num_rows();
      
      if($row_cnt >= 1)
      {
          while ($get_criteria->fetch() )
          { 
              foreach ($sections as $section)
              ?>
                  <p class="haccp-section"><strong><? echo $section; ?></strong></p>
                  <div class="divider"></div>
                  <li><a href="?criteria=<? echo $criteria_db; ?>"><? echo $criteria_db. " ". $hazard_db; ?></a></li> 
              <? 
          }
      }
      $get_criteria->close();
      

      然而,这是对每个标准的标题的回应,而不是标题下面的标题。

      希望有意义并提前感谢!

2 个答案:

答案 0 :(得分:2)

据我了解你的问题。可能是您想根据Section对数据进行分组。对于此尝试下面的代码:

<?php
 $get_criteria = $mysqli->prepare("SELECT criteria, section, hazard FROM haccp ORDER BY section,criteria");

  $get_delivery_criteria->execute();
$get_delivery_criteria->bind_result($criteria_db, $section, $hazard);
$get_delivery_criteria->store_result();
$row_cnt = $get_delivery_criteria->num_rows();
if($row_cnt >= 1)
{
  $current_section="";
    while ($get_delivery_criteria->fetch() )
    { 

      if($section != $current_section)
      { ?>
        <p class="haccp-section"><strong><? echo $section; ?></strong></p>
        <div class="divider"></div>
      <?php 
        $current_section=$section;
      } ?>

      <li><a href="?criteria=<? echo $criteria_db; ?>"><? echo $criteria_db. " ". $hazard; ?></a></li>    
    <? 
    }
    $get_delivery_criteria->close();
}

答案 1 :(得分:0)

@ B.Desai 我已经调整了您的查询以使用$section_db这是不正确的。这现在按预期工作:

                                    $get_criteria = $mysqli->prepare("SELECT criteria, section, hazard FROM haccp ORDER BY criteria");
                                    $get_criteria->execute();
                                    $get_criteria->bind_result($criteria_db, $section_db, $hazard_db);
                                    $get_criteria->store_result();                              
                                    $row_cnt = $get_criteria->num_rows();

                                    if($row_cnt >= 1)
                                    {
                                      $current_section="";
                                        while ($get_criteria->fetch() )
                                        { 
                                          if($section_db != $current_section)
                                          { 
                                          $current_section=$section_db;
                                          ?>
                                            <p class="haccp-section"><strong><? echo $section_db; ?></strong></p>
                                            <div class="divider"></div>
                                          <? 
                                          } 
                                          ?>
                                          <li><a href="?criteria=<? echo $criteria_db; ?>"><? echo $criteria_db. " ". $hazard_db; ?></a></li>    
                                          <? 
                                        }
                                        $get_criteria->close();
                                    }

感谢您的帮助! :)

相关问题