...

View Full Version : Need Ideas for Nested Drop Down Menu



madmatter23
09-05-2007, 01:58 AM
I am very familiar with php but I don't know javascript very well at all, and I suspect I may need to know some for this, so I'm looking for some pointers.

I need to create a set of nested drop down menus that populate using data from a mysql database.

So here's an example.
Drop down menu #1



<select name="category">
<?php
$querylist = "SELECT category FROM categories ORDER BY category ASC";
$list = mysql_query($querylist, $dbc);

echo "<option selected value=\"Select a Category\">Select a Category</option>";
while (list($categories) = mysql_fetch_row($list)) {
echo "<option> $categories </option>";
}
?>
</select>


Then when the user selects a category, I want a second drop down menu with relevant subcategories to appear. It will query the mysql database similarly:



<select name="subcategory">
<?php
$querysublist = "SELECT subcategory FROM subcategories WHERE parent='$category' ORDER BY subcategory ASC";
$sublist = mysql_query($querysublist, $dbc);
while (list($subcategories) = mysql_fetch_row($sublist)) {
echo "<option> $subcategories </option>";
}
?>


Etc. In the end I'd like 3 menus: categories, subcategories, and subsubcategories.

I'm just not quite sure how to make the code recognize that when menu 1 isset, it must then create and populate menu 2, etc.

I'm guessing that I should set php functions to be executed onChange, but I'm not sure how to pass the selection back to the subcategories function, and then make the subcategories function execute correctly.

If anyone has any ideas about how to make this work, I would very much appreciate the advice.

Thank you.

madmatter23
09-05-2007, 03:24 AM
Alright, I figured it out. A little complicated, but it works. I case anyone is interested or wants to use the code, here it is:



$category = $_POST["catselect"];
$subcategory = $_POST["subcatselect"];
$subsubcategory = $_POST["subsubcatselect"];

if (isset($category)){
$catsubmitted = TRUE;
}
if (isset($subcategory)){
$subcatsubmitted = TRUE;
}
if (isset($subsubcategory)){
$subsubcatsubmitted = TRUE;
}

function menu($category, $catsubmitted) {
$dbc = @mysql_connect("localhost", "user", "pw") OR die("We're sorry, the operation could not be completed:" .mysql_error());
mysql_select_db("menus", $dbc) OR die("We're sorry, the operation could not be completed:" .mysql_error());
echo "<option>";
if ($catsubmitted == TRUE){echo $category;}
else {echo "Select a Category";}
echo "</option>";
$querylist = "SELECT name FROM categories ORDER BY name ASC";
$list = mysql_query($querylist, $dbc);
while (list($categories) = mysql_fetch_row($list)) {
echo "<option> $categories </option>";
}
mysql_free_result($list);
}

function submenu($category, $subcategory, $subcatsubmitted) {
$dbc = @mysql_connect("localhost", "user", "pw") OR die("We're sorry, the operation could not be completed:" .mysql_error());
mysql_select_db("menus", $dbc) OR die("We're sorry, the operation could not be completed:" .mysql_error());
echo "<br /><select name=\"subcatselect\" onChange=\"document.categoryform.submit()\"><option>";
if ($subcatsubmitted == TRUE){echo $subcategory;}
else {echo "Select a Subcategory";}
echo "</option>";
$querysublist = "SELECT subcategory FROM subcategories WHERE parent='$category' ORDER BY subcategory ASC";
$sublist = mysql_query($querysublist, $dbc);
while (list($subcategories) = mysql_fetch_row($sublist)) {
echo "<option> $subcategories </option>";
}
echo "</select>";
mysql_free_result($sublist);
}

function subsubmenu($subcategory) {
$dbc = @mysql_connect("localhost", "user", "pw") OR die("We're sorry, the operation could not be completed:" .mysql_error());
mysql_select_db("menus", $dbc) OR die("We're sorry, the operation could not be completed:" .mysql_error());
echo "<br /><select name=\"subsubmcatselect\"><option>Select a Subcategory</option>";
$querysubsublist = "SELECT subsubcategory FROM subsubcategories WHERE subparent='$subcategory' ORDER BY subsubcategory ASC";
$subsublist = mysql_query($querysubsublist, $dbc);
while (list($subsubcategories) = mysql_fetch_row($subsublist)) {
echo "<option> $subsubcategories </option>";
}
echo "</select>";
mysql_free_result($subsublist);
}

?>

<form method="post" action="submit.php" name="categoryform">
<select name="catselect" onChange="document.categoryform.submit()">
<?php echo menu($category, $catsubmitted); ?>
</select>
<?
if ($catsubmitted == TRUE){echo submenu($category, $subcategory, $subcatsubmitted);}
if ($subcatsubmitted == TRUE){echo subsubmenu($subcategory);}
?>
</form>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum