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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:27:25+00:00 2026-05-25T20:27:25+00:00

Doing an allnighter on a project and my mind is blank atm… Simple question

  • 0

Doing an allnighter on a project and my mind is blank atm… Simple question really:

I have two MySQL tables, product and category. Each product belongs to exactly one category. Every category has several products.

SELECT 
  p.uid as product_uid, p.name_NL as product_name, p.price as product_price,
  c.uid as category_uid, c.name_NL as category_name 
FROM 
  product p, category c
WHERE
  p.category_uid = c.uid

This gives me a nice overview of all products in their respective category. My question is about outputting this data on the page. I’m aiming for this:

<h1>Category name</h1>
<p>Product in this category</p>
<p>Other product in this category</p>

<h1>Next category</h1>
<p>Product in next category</p>

My mind is completely blank right now. How would one go about doing this?

I would like to avoid doing subqueries (if possible).

Kind regards,

M

  • 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-25T20:27:26+00:00Added an answer on May 25, 2026 at 8:27 pm

    What about adding ORDER BY category_uid so that the products are ordered by category in your SQL query. Then using PHP, loop through each product (row) and when you encounter a new category, add a new header.

    Example:

    <?php
    
    // ...
    
    $previous_category_uid = null;
    
    // Loop through each row.
    while ( $row = $result->fetch_assoc() )
    {
      // If the category UID is not the same as the one from the previous row, add a header.
      if ( $previous_category_uid != $row['category_uid'] )
      {
        echo '<h1>' . $row['category_name'] . '</h1>';
        $previous_category_uid = $row['category_uid'];
      }
    }
    

    The benefit of this method is that you don’t have to nest queries. A single query will suffice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doing a small project I have received some Java code that I should rewrite
Doing a project with BSTs, I have a logic fault somewhere in my insertion
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
Doing my first tryouts with foreign keys in a mySQL database and are trying
Doing some jquery animation. I have certain divs set up with an attribute of
Doing a project in win32 in c++, attempting to double buffer the image being
Doing a very simple loop to display various data, for _test in @test ...
Doing a code review I've stumbled over GWM in Java-Spring-GWT web-application. As this product
Doing some refactoring in some legacy code I've found in a project. This is
Doing some code reviews lately I came across a number of classes that have

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.