It can be done in one query. Just join the two tables, and do a single loop through that query, printing out each row. When you reach a new category, display it.

Something like this:

$iCategoryID = -1;
while($aRow = ...)
   if($iCategoryID != $aRow['iCategoryID'])
       echo '[',$aRow['sCategoryName'],"]\n";
       $iCategoryID = $aRow['iCategoryID'];
   echo $aRow['sSectionName'],"\n";

That's bare bones, but that is, as far as I know, the best way to do it.


Seona Bellamy wrote:

Hi guys,

I'm relatively new to php, and am pretty much trying to learn as I go along.
I've been struggling to get my head around this problem for a day and half,
so I'm hoping that someone with more familiarity with the language can help

I have the following two tables:

- ID
- CategoryName
- TitleImage

- ID
- CategoryID
- SectionName
- Description

What I want is to get the name of each category and output that, and then
under each category I want to list the sections that belong to it. So for

Catgeory 1
- Section 1
- Section 2
Category 2
- Section 3
- Section 4

Is there a simple way to do this? Do I need to run one query and then run a
second query inside a loop? I have concerns about overheads using this
method, but so far it's the only one I have come up with. I'm open to
suggestions, but please spell everything out clearly because I'm still
learning. :)



<< ella for Spam Control >> has removed Spam messages and set aside Later
for me
You can use it too - and it's FREE!

No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.10 - Release Date: 10/01/2005

-- PHP Database Mailing List ( To unsubscribe, visit:

Reply via email to