|
PHP MySQL Dynamic Drop-Down list
The Problem: A video accessories company had a large list of video camera data(over 500) for their compatibility list. They had a manual system of adding new cameras to an html dropdown list. The current system made it difficult to update and it was prone to innaccuracies. The Solution: I created a MySQL database with an administator interface on the backend and utilizing php and Javascript, I created a Dynamicly generated Drop-down list for the user interface. Here is a sample of the code: <?php ///////// Getting the data from Mysql table for first list box////////// $quer2=mysql_query("SELECT DISTINCT MakeName, MakeID FROM CameraMake order by MakeID"); ///////////// 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=mysql_query("SELECT DISTINCT ModelName,ProductID FROM CameraModel where MakeID=$cat order by ModelName"); }else{$quer=mysql_query("SELECT DISTINCT ModelName,ProductID FROM CameraModel order by ModelName"); } ////////// end of query for second subcategory drop down list box ///////////////////////////
echo "<form method=post name=f1>"; ////////// Starting of first drop downlist ///////// echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>-Camera Make-</option>"; while($noticia2 = mysql_fetch_array($quer2)) { if($noticia2['MakeID']==@$cat){echo "<option selected value='$noticia2[MakeID]'>$noticia2[MakeName]</option>"."<BR>";} else{echo "<option value='$noticia2[MakeID]'>$noticia2[MakeName]</option>";} } echo "</select>"; ////////////////// This will end the first drop down list /////////// echo "<br>"; echo "<br>"; ////////// Starting of second drop downlist ///////// echo "<select name='subcat' onchange=\"reload3(this.form)\"><option value=''>-Camera Model-</option>"; while($noticia = mysql_fetch_array($quer)) { if($noticia['ProductID']==@$cat3){echo "<option value='$noticia[ProductID]'>$noticia[ModelName]</option>"."<BR>";} else{echo "<option value='$noticia[ProductID]'>$noticia[ModelName]</option>";} } echo "</select>"; echo "</form>"; ?>
| |