MYSQL queries
Posted: Thu Jul 05, 2007 10:05 am
Hi,
I am a bit perplexed with this loop problem I have.
I have 3 tables tblCategory and tblSubCategory.
What I want to do is display a list (left menu) of all categories with their sub categories below each parent category.
To get this working I currently use 2 while loops, one to loop the categories and the other to loop the sub categories where related to the parent category
e.g.
Is there a more efficient was of doing this, like within one query?
I am a bit perplexed with this loop problem I have.
I have 3 tables tblCategory and tblSubCategory.
What I want to do is display a list (left menu) of all categories with their sub categories below each parent category.
To get this working I currently use 2 while loops, one to loop the categories and the other to loop the sub categories where related to the parent category
e.g.
Code: Select all
$getCat=mysql_query("SELECT * FROM tblCategory") or bla bla;
while($catRow=mysql_fetch_array($getCat))
{
extract($catRow);
//display the category name
$getSubCat=mysql_query("SELECT * FROM tblSubCategory") or bla bla;
while($catSubRow=mysql_fetch_array($getSubCat))
{
extract($catSubRow);
//display the category name
}
}