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

  • Home
  • SEARCH
  • 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 282467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:17:29+00:00 2026-05-12T05:17:29+00:00

This is probably really obvious and simple – but it isn’t to me with

  • 0

This is probably really obvious and simple – but it isn’t to me with my rudimentary knowledge:)

I’ve got two tables:

table_items
-----------
item_id ¦ item_name ¦ item_describtion ¦ more item stuff.....

table_subitems
-----------------
subitem_id ¦ item_id ¦ subitem_name ¦ More subitem stuff

Each item can have zero to pretty much unlimited subitems, and are related by the fields item_id.

I want to print out the subitems grouped by item.

I am currently doing this with a query which selects subitems where table_items.item_id = table_subitems.item_id and then displays them with a while loop, which is fine as far as it goes, I get something like this:

Item One
========
Subitem 0ne

Item One
========
Subitem two

Item One
========
Subitem three

Item Two
========
Subitem one

Item Three
==========
Subitem one

Item Three
==========

 Subitem two

But I want

ITEM ONE
--------
Subitem one
Subitem two
Subitem three

ITEM TWO
--------
Subitem one

ITEM THREE
----------
Subitem one
Subitem two

How do I do this? Is it a function of the query or of how I show the results?

My knowledge of PHP is growing but still limited, and I’m doing this in a procedural way (ie it’s not OOP, which I am trying to grasp but haven’t fully) so talk to me like I’m stupid.

  • 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-12T05:17:29+00:00Added an answer on May 12, 2026 at 5:17 am

    Use a variable that holds the last heading and print the current heading only if it differs from the last:

    $last = null;
    while ( /* … */ ) {
        if ($row['item_id'] != $last) {
            echo '<hx>'.$row['item_name'].'</hx>';
            $last = $row['item_id'];
        }
        echo $row['subitem_name'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is probably really obvious, but I cannot figure out why I
This is probably a really simple jQuery question, but I couldn't answer it after
This is probably a really simple question but... I would like to do a
Ok, this probably has a really simple answer, but I've never tried to do
This is probably a really simple question, but I don't write stored procedures often
This probably sounds really stupid but I have noo idea how to implement jquery's
This is probably a really stupid newbie-sounding question to you developer type people, but
This is probably a really stupid MSBuild question but if I have <ItemGroup> <Dll
Probably a really simple one this - I'm starting out with C# and need
This probably has a simple answer, but I must not have had enough coffee

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.