Hi, I have a 3D array that I'm trying to traverse and wondering the best way to group results? The query returns

category | item | note
3 | 1200 | socks
3 | 1440 | stockings
8 | 400 | wool hat

How can I group these?

while($row = mysql_fetch_assoc($result)) 
{
		$this_fund = $row['category'];
		if($this_fund != $row['category'])
		{
				echo "\n $this_fund";
				$this_fund = $row['category'];
		}
}

Or build a tough array?

    Can you give us a clearer explanation of what it is you want to do? It's not clear to me what you mean when you say you want to "group results".

      Are you trying to group them by the category in an array?

      <?
      //get result set.
      while($row = mysql_fetch_array($result)){
      
         //add each item_id to the array in corresponding category.
         $this_fund[$row['category_id']][] = $row['item_id'];
      }
      
      
      //you could then cycle through your results, and print them by  the grouping.
      foreach($this_fund as $category_id => $item_key){
      
         echo "Cateogry ".$category_id."<br />";
      
         foreach($this_fund[$category_id] as $item_key => $item_id){
            echo $item_id."<br />";
         }
      
         echo "<br />";
      
      }
      
      ?>
      
      

      That what you're looking for?

        Write a Reply...