Forum Moderators: coopster

Message Too Old, No Replies

multi-level drop-down list

         

dwighty

4:39 pm on Oct 29, 2007 (gmt 0)

10+ Year Member



Hello,

I am looking to build a dynamic drop-down list from a table which we will not know how many levels or items it can return.

The table structure is like this
id, parent_id, menu_name

Each row in the table has a unique id. If the page is in the root dir then the parent_id will be 0. If we have a home page where id=1 and a sub page of the home page then the sub page will have a parent_id of 1.

I want to create a drop-down list so we have a list of possible sections (unique parent_id's) to chose from.

I have done this at the moment based on 2 levels but I am looking to have the script automatically populate the drop-down without limiting it to 2 levels.

Below is what I have so far:

$sql3 = "SELECT id, menu_name FROM ".$site_prefix."_".$tablerows['RefTable']." WHERE parent_id = '0' ORDER BY parent_id ASC";
$result3 = mysql_query($sql3) or die("<div class='notice'>Error: " . mysql_error() ."</div>");

echo "<option value=\"0\" selected><-- Root Dir --></option>";

while($selectrows = mysql_fetch_array($result3)) {

echo "<option value=\"".$selectrows[0]."\">".$selectrows[1]."</option>";

$sql4 = "SELECT id, menu_name FROM ".$site_prefix."_".$tablerows['RefTable']." WHERE parent_id = '".$selectrows[0]."' ORDER BY parent_id ASC";
$result4 = mysql_query($sql4) or die("<div class='notice'>Error: " . mysql_error() ."</div>");

while($subselectrows = mysql_fetch_array($result4)) {
echo "<option value=\"".$subselectrows[0]."\">&nbsp;&nbsp;&nbsp; ".$subselectrows[1]."</option>";
}

}

Thanks in advance

whoisgregg

3:19 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a good thread to read [webmasterworld.com] where they tackle the same basic problem. :)