Jump to content

dynamic dropdown


damian0612

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.