PHPiSean Posted August 12, 2011 Share Posted August 12, 2011 Here's my code <?php //SECOND LINE /*Needs to run 2 queries. One to recieve the main categories and one to recieve the * sub categories. */ $mainlinks = mysql_query("select * from ns_categories"); while ($catlinks = mysql_fetch_array($mainlinks)) { $catlinkname = $catlinks['name']; $catlinkid = $catlinks['id']; $sublinks = mysql_query("select * from ns_subcategories where mainid='$catlinkid'"); echo "<ul>"; echo "<li><a href='products.php?mc=$catlinkid'>$catlinkname</a></li>"; while ($catsublinks = mysql_fetch_array($sublinks)) { $sublinkname = $catsublinks['name']; $sublinkid = $catsublinks['id']; echo "<ul>"; echo "<li><a href='products.php?sc=$sublinkid>$sublinkname</a></li>"; echo "</ul>"; } } echo "</ul>"; ?> It will return all of the main categories, but it will only return the second sub category in the table and not the first. Any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/244575-problem-with-multiple-navigation-queries/ Share on other sites More sharing options...
PHPiSean Posted August 12, 2011 Author Share Posted August 12, 2011 Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/244575-problem-with-multiple-navigation-queries/#findComment-1256503 Share on other sites More sharing options...
TeNDoLLA Posted August 12, 2011 Share Posted August 12, 2011 Ops nevermind. Quote Link to comment https://forums.phpfreaks.com/topic/244575-problem-with-multiple-navigation-queries/#findComment-1256506 Share on other sites More sharing options...
creata.physics Posted August 12, 2011 Share Posted August 12, 2011 Are you sure it's not because you're ending the ul tag in the wrong place? Seems like the code should be: <?php $mainlinks = mysql_query("select * from ns_categories"); while ($catlinks = mysql_fetch_array($mainlinks)) { $catlinkname = $catlinks['name']; $catlinkid = $catlinks['id']; $sublinks = mysql_query("select * from ns_subcategories where mainid='$catlinkid'"); echo "<ul>"; echo "<li><a href='products.php?mc=$catlinkid'>$catlinkname</a></li>"; while ($catsublinks = mysql_fetch_array($sublinks)) { $sublinkname = $catsublinks['name']; $sublinkid = $catsublinks['id']; echo "<ul>"; echo "<li><a href='products.php?sc=$sublinkid>$sublinkname</a></li>"; echo "</ul>"; } echo "</ul>"; } ?> If that is not the issue, I would do a var_dump to check and see what mysql_fetch_array returns, by adding: var_dump($catlinks); under while ($catlinks = mysql_fetch_array($mainlinks)) { And also adding: var_dump($catsublinks); under while ($catsublinks = mysql_fetch_array($sublinks)) { And the array returned from both of them should have all the categories and sub categories that are inside your table, if not, there is an issue with the query, which would not seem right to me as long as the data is actually in there. Let me know! Quote Link to comment https://forums.phpfreaks.com/topic/244575-problem-with-multiple-navigation-queries/#findComment-1256567 Share on other sites More sharing options...
PHPiSean Posted August 12, 2011 Author Share Posted August 12, 2011 Hi after I did the var dump, I found that the strings were actually there. The </ul> surprisingly is in the right place. The thing is, the query will only return the second row, which is odd. If the var_dump returns the values, but doesn't echo them. It must be something wrong with my while statement, but I'm stumped. Quote Link to comment https://forums.phpfreaks.com/topic/244575-problem-with-multiple-navigation-queries/#findComment-1256659 Share on other sites More sharing options...
PHPiSean Posted August 13, 2011 Author Share Posted August 13, 2011 This is irritating, I can't manage to get it to list the other sub categories Quote Link to comment https://forums.phpfreaks.com/topic/244575-problem-with-multiple-navigation-queries/#findComment-1256676 Share on other sites More sharing options...
PHPiSean Posted August 13, 2011 Author Share Posted August 13, 2011 Another problem. The sublinks will have links like products.php?sc=3>Themes</a></li></ul></ul><ul><li><a href= All help is appreciated, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/244575-problem-with-multiple-navigation-queries/#findComment-1256681 Share on other sites More sharing options...
PHPiSean Posted August 13, 2011 Author Share Posted August 13, 2011 I think my problem is related to $sublinks = mysql_query("select * from ns_subcategories where mainid='$catlinkid'"); since it is in the while loop. Would that have any effect on it? If so, where should I put it instead? Quote Link to comment https://forums.phpfreaks.com/topic/244575-problem-with-multiple-navigation-queries/#findComment-1256689 Share on other sites More sharing options...
creata.physics Posted August 16, 2011 Share Posted August 16, 2011 Sorry it took so long for a response. You're while loop seems fine, I'm sure it's a problem rendering the html.. Try this and tell me if it works: $mainlinks = mysql_query("select * from ns_categories"); while ($catlinks = mysql_fetch_array($mainlinks)) { $catlinkname = $catlinks['name']; $catlinkid = $catlinks['id']; $sublinks = mysql_query("select * from ns_subcategories where mainid='$catlinkid' "); $html .= "<ul>"; $html .= "<li><a href='products.php?mc=$catlinkid'>$catlinkname</a></li>"; while ($catsublinks = mysql_fetch_array($sublinks)) { $sublinkname = $catsublinks['name']; $sublinkid = $catsublinks['id']; $html .= "<ul>"; $html .= "<li><a href='products.php?sc=$sublinkid>$sublinkname</a></li>"; $html .= "</ul>"; } } $html .= "</ul>"; echo $html; Edit: Sorry, did not realise the topic was solved, I based this off of the replies, not the SOLVED title. Quote Link to comment https://forums.phpfreaks.com/topic/244575-problem-with-multiple-navigation-queries/#findComment-1258169 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.