开发者

Organizing results by category Redux

I am trying to expand on my question asked here: Creating a multi-dimensional array from query

I have added a category description to my categories table but I cannot figure out how to add it to the code below and display each category description for each category.

Here is the code I am using to display the items by category:

$itemcategories = array();
while ($row = mysqli_fetch_array($result))
{
    $head = $row['category'];
    $itemcategories[$head][] = array(
      'id' => $row['id'],
      'title' => $row['title'],
      'itemdesc' => $row['itemdesc'],
      'price' => $row['price'],
      'special' => $row['special']
  );
}

<?php foreach ($itemcategories as $head => $items) { ?>
    <h3><?php echo $head; ?></h3>
    <table class="chart">
<?php foreach ($it开发者_Go百科ems as $item) { ?>
    <tr><td><?php echo $item['itemdesc']; ?></td></tr>
<?php } ?>
    </table>
<?php } ?>


// to initial variable holder
$categories = $items = array();

// looping mysql result
while ($row = mysqli_fetch_array($result))
{
  // to get category name
  $head = $row['category'];

  // avoid excessive setting of $categories
  if (!isset($categories[$head]))
  {
    $categories[$head] = $row['descrption'];
  }

   // assign mysql result to $items
  $items[$head][] = $row;
}
// free mysql result
mysql_free_result($result);

// loop all $categories
foreach ($categories as $head=>$desc)
{
   // this is my example for printing HTML
   // you can use heredoc syntax
   // or stick to your existing format
   echo "<h3>{$head} - {$desc}</h3><table class="chart">";
   foreach ($items[$head] as $item)
   {
     // print item description
     echo "<tr><td>{$item['itemdesc']}</td></tr>";
   }
   echo "</table>";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜