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

时间:2014-06-12 10:10:34

标签: 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['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

   }
}
}?>

我要做的是将结果分组,以便:

  • 首先显示所有完全匹配
  • 然后我想要除了其中一个关键字之外的所有记录
  • 然后是其余的

目前它们正在以似乎随机的顺序显示。

1 个答案:

答案 0 :(得分:0)

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

如上所述,请尝试使用此代码。

请注意,我已使用HEREDOCS来简化数据解析。