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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.