Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6537271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:35:49+00:00 2026-05-25T10:35:49+00:00

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

  • 0

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 ($items as $item) { ?>
    <tr><td><?php echo $item['itemdesc']; ?></td></tr>
<?php } ?>
    </table>
<?php } ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:35:50+00:00Added an answer on May 25, 2026 at 10:35 am
    // 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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to expand from php. Here is a basic problem I have
I'm trying to expand on an idea from someone else's question, and running into
a month ago I asked this question: Trying to find the top 3 properties
I'm coming from an Objective-C background and am trying to expand my knowledge in
So this question is a sort of follow on from here ( how to
inside a computer....Ok, here is my question. I'm creating a PHP object that will
Sorry I know similar question have been asked many times before but like most
I'm trying to expand my Core Data. So I added a new attribute to
I'm trying to expand navigation options of the context menu on certain elements (specifically,
I've encountered a problem while trying to use the answer from a NDSolve in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.