scmeeker Posted July 27, 2010 Share Posted July 27, 2010 I'm trying to create a dependent drop down list. If a user picks something from the initial category list, I want the subcategory list to change to populate with its relevant category ID. Kind of like picking a state from the first drop down then the next would populate with all the state's cities. When I print this, it lists individual drop down lists rather than one. Is there another way I should be looking at this? Thanks for your suggestions. //Set the selected category $selected_cat = (isset($_GET["cat_id"])) ? $_GET["cat_id"] : false; //show categories first $result = mysql_query("SELECT cat_id, cat_title FROM category ORDER BY cat_id") or die(mysql_error()); if (mysql_num_rows($result) < 1) { $categoryList = "<p><em>Sorry, no categories to browse.</em></p>"; } else { //Display the categories while ($cats = mysql_fetch_array($result)) { $cat_id = $cats['cat_id']; $cat_title = $cats['cat_title']; $categoryList .= "<select name=\"cat_id\" class=\"blackfont\"> <option value=$cat_id>$cat_title</option></select>"; if ($cats['cat_id']==$selected_cat) { //get items $get_items_sql = mysql_query("SELECT id, cat_id, sub_title FROM sub_category WHERE cat_id = '{$selected_cat}'") or die(mysql_error()); if (mysql_num_rows($get_items_sql) < 1) { $content = "<p><em>Sorry, no items in this category.</em></p>\n"; } else { while ($items = mysql_fetch_array($get_items_sql)) { $cat_id = $cats['cat_id']; $sub_title = $cats['sub_title']; $content .= "<select name=\"sub_id\" class=\"blackfont\"> <option value=$cat_id>$sub_title</option></select>"; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 27, 2010 Share Posted July 27, 2010 Not tested, so there may be some syntax errors //Run query to get categories $query = "SELECT cat_id, cat_title FROM category ORDER BY cat_id"; $result = mysql_query($query) or die(mysql_error()); if (!mysql_num_rows($result)) { $categoryList = "<p><em>Sorry, no categories to browse.</em></p>"; } else { //Set the selected category (if exist) $selected_cat = (isset($_GET["cat_id"])) ? $_GET["cat_id"] : false; //Create category select list $categoryList = "<select name=\"cat_id\" class=\"blackfont\">\n"; //Display the categories while ($cats = mysql_fetch_array($result)) { $selected = ($cats['cat_id']==$selected_cat) ? ' selected="selected"' : ''; $categoryList .= "<option value=\"{$cats['cat_id']}\"{$selected}>{$cats['cat_title']}</option>\n"; //Set first category as selected if none selected, so we can determine subcategories to display if($selected_cat===false) { $selected_cat = $cats['cat_id']; } } $categoryList .= "</select>\n"; //Run query to get subcategories $query = "SELECT id, cat_id, sub_title FROM sub_category WHERE cat_id = '{$selected_cat}'"; $result = mysql_query($query) or die(mysql_error()); if (!mysql_num_rows($result)) { $subCategoryList = "<p><em>Sorry, no categories to browse.</em></p>"; } else { //Create subcategory select list (if needed) $subCategoryList = "<select name=\"sub_id\" class=\"blackfont\">\n"; while ($subcats = mysql_fetch_array($result)) { $subCategoryList .= "<option value=\"{$subcats['cat_id']}\">{$subcats['sub_title']}</option>\n"; } $subCategoryList .= "</select>\n"; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.