Re: Trying to figure out the best way to get and output data

From: Date: Wed, 12 Jan 2005 06:15:25 +0000
Subject: Re: Trying to figure out the best way to get and output data
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-38114@lists.php.net to get a copy of this message
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. Chris 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 me. I have the following two tables: CATEGORY -------- - ID - CategoryName - TitleImage SECTION ------- - 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 example: 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. :) Cheers, Seona. __________________________________________________________________ << ella for Spam Control >> has removed Spam messages and set aside Later for me You can use it too - and it's FREE! http://www.ellaforspam.com -- 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


« previous php.db (#38114) next »