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 984273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:59:21+00:00 2026-05-16T04:59:21+00:00

I have a table which contains a list of categories and and another table

  • 0

I have a table which contains a list of categories and and another table which contains a list of products in each category.

e.g.

CatID | Cat_Name
----------------
1     | Books
2     | CDs

and

ProductID | Product_Name     | CatID
------------------------------------
1         |The Bible         | 1
2         |The Koran         | 1
3         |90s Greatest Hits | 2
4         |80s Greatest Hits | 2

what I want to do is get

<ul>
<li>Books</li>
   <ul>
   <li>The Bible</li>
   <li>The Koran</li>
   </ul>
<li>Cds</li>
   <ul>
   <li>90s Greatest Hits </li>
   <li>00s Greatest Hits </li>
   </ul>
 </ul>

without doing (PHP)

$query = mysql_query("SELECT ... FROM categories")

while($row = mysql_fetch_assoc($query)):

$query2 = mysql_query("SELECT ... FROM products WHERE catId = $row['CatId'])

endwhile;

How can I move away from using nested while/SELECT statements, can I do it efficiently with one query – and how do I access the relevant data using PHP?

EDIT: I was under the impression that if your tables become quite large, multiple SQL queries will slow your page, and therefore to optimize page load you should try to reduce the number of queries?

  • 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-16T04:59:22+00:00Added an answer on May 16, 2026 at 4:59 am

    select all products and category names using JOIN

    select * from category c, products p where c.catID = p.catID order by p.catID
    

    fetch and group results

    $products_by_cat = array();
    foreach(fetch(....) as $record)
        $products_by_cat[$record->catName][] = $record;
    

    you’ll get a 2-D array like

     [books] => array(bible, koran)
     [CDs]   => array(cd1, cd2, ....)
    

    output this array with two nested loops

     foreach($products_by_cat as $cat => $products)
        <li> $cat  <ul>
        foreach($products as $p)
            <li> $p->name </li>
        </li></ul>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which contains a list of products for a company. They
I have a MySQL table which contains a list of all possible products a
I have a table which contains a list of Surveys (PK is ID) CREATE
I have one table which contains events and dates, and another which contains the
I have a table which contains user choices of a certain type... with each
I have a table called changes which contains a list of record numbers the
I have a products table which contains duplicate products by a column id_str and
I have a table which contains a list of ID's (int) and the date
I have a table A which contains a list, a table B which contains
I have a Category class which looks like this: @Entity @Table(name = categories) public

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.