shadiadiph Posted December 28, 2008 Share Posted December 28, 2008 Hi I am trying to create a dynamic drop down for my forms from an sql database does anyone know of any good tutorials that are not to hard to get your head around? For example a drop down country which will automatically change the states drop down box value or category and subcategory Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/ Share on other sites More sharing options...
revraz Posted December 28, 2008 Share Posted December 28, 2008 It's really just a matter of reading your DB, and doing a loop and populating your OPTION tag. Which part are you stuck on? Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-724906 Share on other sites More sharing options...
shadiadiph Posted December 28, 2008 Author Share Posted December 28, 2008 hi revraz I was using a script but i do not understand it I have a categories and subcategories fields in my forms I used someone elses script but can't get my head around it like when the user wants to edit the details i can't get it to load with the values they already selected Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-724912 Share on other sites More sharing options...
revraz Posted December 28, 2008 Share Posted December 28, 2008 Explain what you mean by already selected. Are these selections stored in a DB or do you mean selected in a HTML tag? Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-724914 Share on other sites More sharing options...
shadiadiph Posted December 28, 2008 Author Share Posted December 28, 2008 they are already stored in the database so i want them to load the reload the drop down boxes with the items in the database selected Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-724915 Share on other sites More sharing options...
revraz Posted December 28, 2008 Share Posted December 28, 2008 Ok, so use code that Reads the DB for your field. Do a while loop and put into an array In your while loop, population the OPTION tag Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-724918 Share on other sites More sharing options...
shadiadiph Posted December 28, 2008 Author Share Posted December 28, 2008 sorry totally lost me. the original code <? $quer2=mysql_query("SELECT DISTINCT category,intCatID FROM tblcatdetails order by category"); if(isset($cat) and strlen($cat) > 0){ $quer=mysql_query("SELECT DISTINCT subcategory FROM tblsubcatdetails where intCatID=$cat order by subcategory"); }else{$quer=mysql_query("SELECT DISTINCT subcategory FROM tblsubcatdetails order by subcategory"); } echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>"; while($noticia2 = mysql_fetch_array($quer2)) { if($noticia2['intCatID']==@$cat){echo "<option selected value='$noticia2[category]'>$noticia2[category]</option>"."<BR>";} else{echo "<option value='$noticia2[intCatID]'>$noticia2[category]</option>";} } echo "</select>"; ?> </td></tr> <tr><td><span id="t_subcat">SUB CATEGORY</span></td></tr> <tr><td> <? 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>"; ; ?> i updated it to this <? $quer2=mysql_query("SELECT DISTINCT category,intCatID FROM tblcatdetails where category='$category'"); if(isset($cat) and strlen($cat) > 0){ $quer=mysql_query("SELECT DISTINCT subcategory FROM tblsubcatdetails where where subcategory='$subcategory'"); }else{$quer=mysql_query("SELECT DISTINCT subcategory FROM tblsubcatdetails order by subcategory"); } echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>"; while($noticia2 = mysql_fetch_array($quer2)) { if($noticia2['intCatID']==@$cat){echo "<option selected value='$noticia2[category]'>$noticia2[category]</option>"."<BR>";} else{echo "<option value='$noticia2[intCatID]'>$noticia2[category]</option>";} } echo "</select>"; ?> </td></tr> <tr><td><span id="t_subcat">SUB CATEGORY</span></td></tr> <tr><td> <? 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>"; ; ?> think i must have somethng wrong in my thinking? here is the javascript in the head too <SCRIPT language=JavaScript> <!-- function reload(form) { var val=form.cat.options[form.cat.options.selectedIndex].value; self.location='updatead.php?cat=' + val ; } function disableselect() { <? if(isset($cat) and strlen($cat) > 0){ echo "document.postad.subcat.disabled = false;";} else{echo "document.postad.subcat.disabled = true;";} ?> } //--> can you please tell me if i am on the right track with this i have no idea never done anything like this before Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-724920 Share on other sites More sharing options...
shadiadiph Posted December 28, 2008 Author Share Posted December 28, 2008 I already defined and called category and subcategory values Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-724923 Share on other sites More sharing options...
ds111 Posted December 28, 2008 Share Posted December 28, 2008 code for that would be: <?php connect_db(); //or other db connection here $query = "SELECT * from TABLE WHERE condition"; //edit table, ignore WHERE condition if no condition $run_query =mysql_query($query); //run the query; $selected = $row['selected']; //Change to get value of which one is selected echo '<select name="name" id="id">'; //Start the select. Edit to ur desire while($row = mysql_fetch_array($run_query)) { if($selected == $row['ROW']) { echo '<option selected="selected" value="'.$row['ROW'].'">'.$row['ROW'].'"</option>'; // Loops and makes the selections } else { echo '<option value="'.$row['ROW'].'">'.$row['ROW'].'"</option>'; // Loops and makes the selections } } ^^ That is all for looping and getting selected one from DB. Make sure u edit "ROW", $selected, and insert DB details. Good luck Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-724924 Share on other sites More sharing options...
shadiadiph Posted December 28, 2008 Author Share Posted December 28, 2008 thanks works great except for one thing in the select box it shoes the option for example Boats" and his has an unwanted " next to the option value? Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-724932 Share on other sites More sharing options...
shadiadiph Posted December 28, 2008 Author Share Posted December 28, 2008 also another problem doesn't display the rest of the items from the dropdown list if the user wants to change the value? <? $query = "SELECT * from tblcatdetails WHERE category='$category'"; //edit table, ignore WHERE condition if no condition $run_query =mysql_query($query); //run the query; $selected = $row['category']; //Change to get value of which one is selected echo '<select name="name" id="id">'; //Start the select. Edit to ur desire while($row = mysql_fetch_array($run_query)) { if($selected == $row['category']) { echo '<option selected="selected" value="'.$row['category'].'">'.$row['category'].'"</option>'; // Loops and makes the selections } else { echo '<option value="'.$row['category'].'">'.$row['category'].'"</option>'; // Loops and makes the selections } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-724933 Share on other sites More sharing options...
ds111 Posted December 28, 2008 Share Posted December 28, 2008 thanks works great except for one thing in the select box it shoes the option for example Boats" and his has an unwanted " next to the option value? yes of course here: change : echo '<option value="'.$row['ROW'].'">'.$row['ROW'].'"</option>'; // Loops and makes the selections to: echo '<option value="'.$row['ROW'].'">'.$row['ROW'].'</option>'; // Loops and makes the selections sorry about that. Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-725026 Share on other sites More sharing options...
shadiadiph Posted December 28, 2008 Author Share Posted December 28, 2008 thanks i got that a while ago Quote Link to comment https://forums.phpfreaks.com/topic/138646-solved-does-anyone-know-of-any-good-select-from-sql-drop-down-form-tutorials/#findComment-725032 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.