mlee Posted October 21, 2015 Share Posted October 21, 2015 Hi all! I want to display information online to the user by using drop downs to narrow things down. ex: User selects a country, then a list of cities displays, pick a city then a list of services display, pick a service and you get all the options displayed. That's it, no adding or modifying. I came across this code below which is using several tables. I only have one table. Can someone please help me customize this code so it works with my one table and fields? I'm new at this and will learn a lot from comparing the before and after. I have one table (called extras) which has 5 fields and almost 20000 records: My fields are: ID (unique, primary) Country Destination Service Option dd3.php: <?php require "config.php"; // Your Database details ?> <!doctype html public "-//w3c//dtd html 3.2//en"> <html> <head> <SCRIPT language=JavaScript> function reload(form) { var val=form.cat.options[form.cat.options.selectedIndex].value; self.location='dd3.php?cat=' + val ; } function reload3(form) { var val=form.cat.options[form.cat.options.selectedIndex].value; var val2=form.subcat.options[form.subcat.options.selectedIndex].value; self.location='dd3.php?cat=' + val + '&cat3=' + val2 ; } </script> </head> <body> <?Php ///////// Getting the data from Mysql table for first list box////////// $quer2="SELECT DISTINCT category,cat_id FROM category order by category"; ///////////// End of query for first list box//////////// /////// for second drop down list we will check if category is selected else we will display all the subcategory///// $cat=$_GET['cat']; // This line is added to take care if your global variable is off if(isset($cat) and strlen($cat) > 0){ $quer="SELECT DISTINCT subcategory,subcat_id FROM subcategory where cat_id=$cat order by subcategory"; }else{$quer="SELECT DISTINCT subcategory,subcat_id FROM subcategory order by subcategory"; } ////////// end of query for second subcategory drop down list box /////////////////////////// /////// for Third drop down list we will check if sub category is selected else we will display all the subcategory3///// $cat3=$_GET['cat3']; // This line is added to take care if your global variable is off if(isset($cat3) and strlen($cat3) > 0){ $quer3="SELECT DISTINCT subcat2 FROM subcategory2 where subcat_id=$cat3 order by subcat2"; }else{$quer3="SELECT DISTINCT subcat2 FROM subcategory2 order by subcat2"; } ////////// end of query for third subcategory drop down list box /////////////////////////// echo "<form method=post name=f1 action='dd3ck.php'>"; ////////// Starting of first drop downlist ///////// echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>"; foreach ($dbo->query($quer2) as $noticia2) { 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' onchange=\"reload3(this.form)\"><option value=''>Select one</option>"; foreach ($dbo->query($quer) as $noticia) { if($noticia['subcat_id']==@$cat3){echo "<option selected value='$noticia[subcat_id]'>$noticia[subcategory]</option>"."<BR>";} else{echo "<option value='$noticia[subcat_id]'>$noticia[subcategory]</option>";} } echo "</select>"; ////////////////// This will end the second drop down list /////////// ////////// Starting of third drop downlist ///////// echo "<select name='subcat3' ><option value=''>Select one</option>"; foreach ($dbo->query($quer3) as $noticia) { echo "<option value='$noticia[subcat2]'>$noticia[subcat2]</option>"; } echo "</select>"; ////////////////// This will end the third drop down list /////////// echo "<input type=submit value='Submit the form data'></form>"; ?> <br><br> <a href=dd3.php>Reset and Try again</a> </body> </html> -------------------------------------------------------------------------------------------------------------------------------------------- dd3ck.php: <!doctype html public "-//w3c//dtd html 3.2//en"> <html> <head> <title>Multiple drop down list box from plus2net</title> </head> <body> <?Php $cat=$_POST['cat']; $subcat=$_POST['subcat']; $subcat3=$_POST['subcat3']; echo " cat=$cat <br> subcat=$subcat <br> subcat3= $subcat3 "; ?> <br><br> <a href=dd3.php>Reset and Try again</a> <br><br> </body> </html> Your help is much appreciated! Regards, Mel Quote Link to comment Share on other sites More sharing options...
TapeGun007 Posted October 21, 2015 Share Posted October 21, 2015 Hi Mel, I recently wrote a similar thing except I had a drop down with all the US States and it auto populated a drop down with all the counties in that state. I can tell you it's much easier and readable if you use multiple tables to accomplish this. And rather than use php, you'll really need some jquery or ajax. Here is a website that may answer this for you: http://www.php-dev-zone.com/2013/10/country-state-city-dropdown-using-ajax.html Quote Link to comment Share on other sites More sharing options...
Barand Posted October 21, 2015 Share Posted October 21, 2015 You really should normalize your data. Relational databases are not for storing spreadsheets. That said, with the data you currently have, your first query would be SELECT DISTINCT country FROM extras ORDER BY country When you know the country, the next query would be SELECT DISTINCT destination FROM extras WHERE country = ? ORDER BY destination ... and so on 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.