Desdinova Posted April 2, 2006 Share Posted April 2, 2006 Ok well this is the case.I have a mysql database, with a table Menuitems: ID, Name, LinkedID.The idea is this,a menuitem gets an incremented ID, a name, and a linkedID. The linkedID shows which ID is the menu's parent. Now I want the childs to go infinitely deep, meaning I need some sort of query loop.Brief example:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]ID----Name--------LinkedID1-----MenuA----------0------2-----MenuB----------0------3-----SubmenuA-----1------4-----SubmenuAA----3-----[/quote]This would produce:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]MenuA--SubmenuA----SubmenuAAMenuB[/quote]As you can see, if LinkedID = 0, the menu is not a child, else it is.So everytime LinkedID != 0, a query must be called.How can I loop this? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted April 2, 2006 Share Posted April 2, 2006 you should be able to set this up with a few lines of code$query = "SELECT * FROM table WHERE linkId = 0";$result = mysql_query($query) or die ("COULD NOT RUN QUERY <br />".mysql_error());while($row = mysql_fetch_array($result)){//print something$query = "SELECT * FROM table WHERE linkId = ". $row['id']. "";$result = mysql_query($query) or die ("COULD NOT RUN QUERY <br />".mysql_error());//print somehting}there should be a way to do this without having to run so many queries, like putting all of the data in an array and looping through that array placing zero items at zero and their children under that Quote Link to comment Share on other sites More sharing options...
Desdinova Posted April 2, 2006 Author Share Posted April 2, 2006 Yes I had it like that. But the problem with this is, that it only gets as much childmenu's as the amount of queries I put in it, thus not making it infinitely deep. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 2, 2006 Share Posted April 2, 2006 You need to put the second query in a loop. Something like:[code]<?php$query = "SELECT * FROM table WHERE linkId = 0";$result = mysql_query($query) or die ("COULD NOT RUN QUERY <br />".mysql_error());while($row = mysql_fetch_assoc($result)){//print something $linkId = $row['id']; while ($linkId != 0) { $subquery = "SELECT * FROM table WHERE linkId = ". $row['id']. ""; $subresult = mysql_query($subquery) or die ("COULD NOT RUN QUERY <br />".mysql_error()); $subrow = mysql_fetch_assoc($subresult); // do something with the results $linkId = $subrow['id']; }}?>[/code]Ken Quote Link to comment 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.