Jump to content

Infinite query


Desdinova

Recommended Posts

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--------LinkedID
1-----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
----SubmenuAA
MenuB
[/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?
Link to comment
https://forums.phpfreaks.com/topic/6395-infinite-query/
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/6395-infinite-query/#findComment-23136
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/6395-infinite-query/#findComment-23163
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.