Php 在while循环中对数据库结果进行分组

Php 在while循环中对数据库结果进行分组,php,while-loop,grouping,Php,While Loop,Grouping,我正在运行while循环以返回mysqli查询的所有结果,但我想使用php对结果进行分组 我的代码是 while($row = mysqli_fetch_array($result)) { if($row['total'] >0)//row['total'] is found out within the query itself { $recipeWords = str_word_count(preg_replace('/\s{1,}(and\s*)*/', ' ', $row['

我正在运行while循环以返回mysqli查询的所有结果,但我想使用php对结果进行分组

我的代码是

while($row = mysqli_fetch_array($result))
{
if($row['total'] >0)//row['total'] is found out within the query itself
{

  $recipeWords = str_word_count(preg_replace('/\s{1,}(and\s*)*/', ' ', $row['title']));
  if($countTerms == $recipeWords)
  {
      echo "<h2>Exact Match</h2>";?>
      <div class="fluid recipeContainerSmall">
      <a href="recipe_details.php?id=<?php echo $row['recipeID']?>">
      <img src="images/recipes/<?php echo $row['image']?>" alt="<?php echo $row['title']?>" title="<?php echo $row['title']?>" />
      <h4><?php echo $row['title']?></h4></a> 
      </div><?php
  }
  elseif($countTerms == ($recipeWords-1))
  {
      echo "<h2>Closest Matches</h2>";?>
      <div class="fluid recipeContainerSmall">
      <a href="recipe_details.php?id=<?php echo $row['recipeID']?>">
      <img src="images/recipes/<?php echo $row['image']?>" alt="<?php echo $row['title']?>" title="<?php echo $row['title']?>" />
      <h4><?php echo $row['title']?></h4></a> 
      </div><?php
   }
   else
   {
      echo "<h2>Other Suggestions</h2>";?>
      <div class="fluid recipeContainerSmall">
      <a href="recipe_details.php?id=<?php echo $row['recipeID']?>">
      <img src="images/recipes/<?php echo $row['image']?>" alt="<?php echo $row['title']?>" title="<?php echo $row['title']?>" />
      <h4><?php echo $row['title']?></h4></a> 
      </div><?php

   }
}
}?>
我尝试将结果分组,以便:

首先显示所有精确匹配 然后我想要所有的记录,除了一个关键字外,所有的记录都可以找到 然后剩下的 目前,它们的显示顺序似乎是随机的

<?php
while($row = mysqli_fetch_array($result))
{
if($row['total'] >0)//row['total'] is found out within the query itself
{
  $recipeWords = str_word_count(preg_replace('/\s{1,}(and\s*)*/', ' ', $row['title']));

  $exactMatches = "";
  $closestMatches = "";
  $otherSuggestions = "";

  if($countTerms == $recipeWords)
  {
      $exactMatches .= <<<EXACT_MATCH
          <h2>Exact Match</h2>
          <div class="fluid recipeContainerSmall">
          <a href="recipe_details.php?id={$row['recipeID']}">
          <img src="images/recipes/{$row['image']}" alt="{$row['title']}" title="{$row['title']}" />
          <h4>{$row['title']}</h4></a> 
          </div>
EXACT_MATCH;
  }
  elseif($countTerms == ($recipeWords-1))
  {
      $closestMatches .= <<<CLOSEST_MATCH
          <h2>Closest Match</h2>
          <div class="fluid recipeContainerSmall">
          <a href="recipe_details.php?id={$row['recipeID']}">
          <img src="images/recipes/{$row['image']}" alt="{$row['title']}" title="{$row['title']}" />
          <h4>{$row['title']}</h4></a> 
          </div>
CLOSEST_MATCH;
   }
   else
   {

    $otherSuggestions .= <<<OTHER_SUGGESTIONS
      <h2>Other Suggestions</h2>
      <div class="fluid recipeContainerSmall">
      <a href="recipe_details.php?id={$row['recipeID']}">
      <img src="images/recipes/{$row['image']}" alt="{$row['title']}" title="{$row['title']}" />
      <h4>{$row['title']}</h4></a> 
      </div>
OTHER_SUGGESTIONS;
   }

   // ECHO in the order you want to...

   echo $exactMatches . $closestMatches . $otherSuggestions;
}
}?>
如您上面所问,请尝试此代码


请注意,我曾经简化过数据的解析。

将它们存储在一些变量中,而不是打印它们,然后打印变量。示例:declare:$closestMatches=$其他建议=$精确匹配=;然后,在每个if中,只需将其附加到变量:example:$exactMatches.=exactMatches的内容,而不是回显内容。以这种方式访问行:{$row['YourRow']}将尝试一下,谢谢!如果你想,我可以解析你需要尝试的全部代码。不管怎么说,逻辑很简单:是的,如果你愿意,那就太好了,我也会尝试一下,看看我能不能让它工作起来,这也会是一个很好的学习方法:谢谢!谢谢你,伙计,这基本上就是我想出的减去herdocs的用法:@user2886669:herdocs不是必需的,也没有太多的使用,但我很有信心,在这种情况下,它们可能非常有用,以便不必更改大部分代码:P