xox Posted February 1, 2011 Share Posted February 1, 2011 I have two tables categories and subcategories and what I want do is display them but here I ran into problem displaying the subcategories. Here's the code that doesn't work, all it does it displays categories. $query = "SELECT ID,name FROM `categores`"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo $row['name']; $id_main=$row['ID']; $query2 ="SELECT name_subcategory FROM subcategories WHERE id_main_category=".$row['ID']; $result2 = mysql_query($query2) or die(mysql_error()); while ($row2=mysql_fetch_array($result2)); { echo $row2['name_subcategory']; } echo "<br />"; } Display should be -category --subcategory --subcategory -another category --subcategory of "another category" . . . Quote Link to comment https://forums.phpfreaks.com/topic/226364-displaying-links-depending-on-id-from-another-table/ Share on other sites More sharing options...
Psycho Posted February 1, 2011 Share Posted February 1, 2011 Learn how to do JOINS - never run queries in a loop - it is a huge performance hit on the server. Below is a much better approach, but I don't know why your code wasn't showing the sub categories. Most likely, the data is not properly "linked" in the database. $query = "SELECT c.ID, c.name, sc.name_subcategory FROM `categores` AS c JOIN `subcategories` AS sc ON sc.id_main_category = c.ID"; $result = mysql_query($query) or die(mysql_error()); //Flag to determine change in category $currentCatID = false; //Process the records while($row = mysql_fetch_array($result)) { //Test if this is a new category from the previous if($currentCatID != $row['ID']) { //Display the category name and set flag echo "<br /><b>{$row['name']}</b><br />\n"; $currentCatID = $row['ID']; } //Display the subcategory echo "{$row['name_subcategory']}<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/226364-displaying-links-depending-on-id-from-another-table/#findComment-1168392 Share on other sites More sharing options...
sunfighter Posted February 1, 2011 Share Posted February 1, 2011 MySQL Case-sensitivity issues. They do exist, but why IDK. Try id instead of ID. Quote Link to comment https://forums.phpfreaks.com/topic/226364-displaying-links-depending-on-id-from-another-table/#findComment-1168402 Share on other sites More sharing options...
Psycho Posted February 1, 2011 Share Posted February 1, 2011 MySQL Case-sensitivity issues. They do exist, but why IDK. Try id instead of ID. If he was referencing fields that do not exist, his queries would be failing. Quote Link to comment https://forums.phpfreaks.com/topic/226364-displaying-links-depending-on-id-from-another-table/#findComment-1168428 Share on other sites More sharing options...
xox Posted February 1, 2011 Author Share Posted February 1, 2011 Thanks for point me to JOINS, I'll get reading right away. You helped me a lot! Quote Link to comment https://forums.phpfreaks.com/topic/226364-displaying-links-depending-on-id-from-another-table/#findComment-1168431 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.