damian0612 Posted January 23, 2010 Share Posted January 23, 2010 Hi I am trying to create 2 dropdown lists for a category & sub_category. The options are stored in one mysql table and i want the relevant sub_category to display depending on what category has been selected. I found some code that does this if they category & sub_category are held in different tables but I'm not sure what I would need to change to make it work within just 1 table. Here is the code: http://www.plus2net.com/php_tutorial/php_drop_down_list.php Any help would be appreciated, thanks Quote Link to comment https://forums.phpfreaks.com/topic/189524-dynamic-dropdown/ Share on other sites More sharing options...
JAY6390 Posted January 23, 2010 Share Posted January 23, 2010 I've not looked at the code in the article, because if you want specific code help you need to post it. Basically though, the top level categories will not have a parent, so you need to select all the rows with no parents for the main categories, and then select the subcategories based on the parents id for the main categories Quote Link to comment https://forums.phpfreaks.com/topic/189524-dynamic-dropdown/#findComment-1000394 Share on other sites More sharing options...
damian0612 Posted January 23, 2010 Author Share Posted January 23, 2010 Hi Thanks for the reply but I'm not sure how to do as you have advised. Here is the code I have & I'd appreciate any help in changing the code to meet my needs: $quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); $cat=$_GET['cat']; //This line is added to take care if your global variable is off if(strlen($cat) > 0 and !is_numeric($cat)){//check if $cat is numeric data or not. echo "Data Error"; exit; } if(isset($cat) and strlen($cat) > 0){ $quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"); }else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); } echo "<form method=post name=f1 action=''>"; /// Add your form processing page address to action in above line. Example action=dd-check.php//// ////////// Starting of first drop downlist ///////// echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>"; while($noticia2 = mysql_fetch_array($quer2)) { if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";} else{echo "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";} } echo "</select>"; ////////////////// This will end the first drop down list /////////// ////////// Starting of second drop downlist ///////// echo "<select name='subcat'><option value=''>Select one</option>"; while($noticia = mysql_fetch_array($quer)) { echo "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>"; } echo "</select>"; ////////////////// This will end the second drop down list /////////// // add your other form fields here //// echo "<input type=submit value=Submit>"; echo "</form>"; Quote Link to comment https://forums.phpfreaks.com/topic/189524-dynamic-dropdown/#findComment-1000415 Share on other sites More sharing options...
bl00dshooter Posted January 23, 2010 Share Posted January 23, 2010 http://www.w3schools.com/ajax/ajax_database.asp Try adapting it to your needs. Quote Link to comment https://forums.phpfreaks.com/topic/189524-dynamic-dropdown/#findComment-1000450 Share on other sites More sharing options...
damian0612 Posted January 23, 2010 Author Share Posted January 23, 2010 Thanks bloodshooter but I'd prefer to try and continue my learning using the current method, cheers Quote Link to comment https://forums.phpfreaks.com/topic/189524-dynamic-dropdown/#findComment-1000458 Share on other sites More sharing options...
jl5501 Posted January 23, 2010 Share Posted January 23, 2010 Your category table will need to have a field called parent_cat or something similar to tell you the parent category for each category. The top level or main categories will have this field set to 0. So for your main category dropdown the query will be select from category where parent_id = 0; When you have a main cat selected, then its id can be checked in the sub category query so select from category where parent_id = maincatid. I use this approach of having all cats in one table and it works for as many levels of subcategory as you like. I also have a set of my own functions that I use to represent the category tree in various ways, like for dropdowns, a category navigation column or whatever. Quote Link to comment https://forums.phpfreaks.com/topic/189524-dynamic-dropdown/#findComment-1000514 Share on other sites More sharing options...
damian0612 Posted January 24, 2010 Author Share Posted January 24, 2010 Thanks for that advice, done as you have said and it works perfectly now, cheers and thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/189524-dynamic-dropdown/#findComment-1000751 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.