Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Need Ideas for Nested Drop Down Menu

    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

    Code:
    <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:

    Code:
    <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.
    Last edited by madmatter23; 09-05-2007 at 03:12 AM.

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Got It

    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:

    Code:
    $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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •