I am trying to create dynamic categories nav for a gallery page.
The structure of the nav would be:
<ul> <li><a href="#">Category1</a> <ul> <li><a href="#">SubCategory1</a></li> <li><a href="#">SubCategory2</a></li> <li><a href="#">SubCategory3</a></li> </ul> </li> <li><a href="#">Category2</a> <ul> <li><a href="#">SubCategory1</a></li> <li><a href="#">SubCategory2</a></li> <li><a href="#">SubCategory3</a></li> </ul> </li> </ul>
The current tables I created in my database are:
categories idCat Category
subcategories idSub subCategory
cat_sub idCat idSub
And what I have so far in PHP and MySQL statements is:
<?php $con = mysql_connect("localhost", "xray", "password") or die('Could not connect to server'); mysql_select_db("xray", $con) or die('Sorry, could not connect to the database'); $query = "SELECT categories.Category, categories.idCat, subcategories.subCategory, subcategories.idSub FROM categories JOIN cat_sub ON categories.idCat = cat_sub.idCat JOIN subcategories ON subcategories.idSub = cat_sub.idSub"; $result = mysql_query($query) or die('Error'); while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ } ?>
I think I am probably using the wrong join, as its returning each Category several times. And I am not sure how to structure the while loop to achieve the above mentioned structure. Any advice would be much appreciated.
Thanks, Aleks