spires Posted August 22, 2006 Share Posted August 22, 2006 Hi, I'm trying to create 2 dropdown menus.both will take data from the same table but from different columns.one will displays all of the values in 'type' column and the second will displays all of the values in the 'cat' column I can get either one on its own to work, but not together. The problem is: In both dropdown menues i have a while loop (going through all the values in the table). But the both contain a mysql_fetch_array().Is there any way around this. select from database.[code]$query = "SELECT * FROM type order by id DESC";$result = mysql_query($query) or die ("query 1 failed");$count = mysql_num_rows($result);[/code]type column[code]<?php echo '<form name="type1" method="post" action=""> <select name="type"> <option value="NULL" selected>- - - - - - - </option>'; while ($row = mysql_fetch_array($result)) { echo '<option value="'.$row['type'].'">'.$row['type'].'</option>'; } echo '</select> </form>'; ?> [/code]cat column[code] <?php echo '<form name="cat1" method="post" action=""> <select name="cat"> <option value="NULL" selected>- - - - - - - </option>'; while ($row1 = mysql_fetch_array($result)) { echo '<option value="'.$row1['cat'].'">'.$row1['cat'].'</option>'; } echo '</select> </form>'; ?>[/code]maybe a foreach loop?But, i'm not to sure how they work.Thanks Link to comment https://forums.phpfreaks.com/topic/18321-creating-2-drop-down-menues-from-the-same-table/ Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Share Posted August 22, 2006 [code]<?php $cathtml='<select name="cat"><option value="null" selected="selected">- - - - - - - -</option>'; $typehtml='<select name="type"><option value="null" selected="selected">- - - - - - - -</option>'; $query = mysql_query("SELECT * FROM type order by id DESC"); while ($row=mysql_fetch_array($query)) { $cathtml.='<option value="'.$row['cat'].'">'.$row['cat'].'</option>'; $typehtml.='<option value="'.$row['type'].'">'.$row['type'].'</option>'; } $cathtml.='</select>'; $typehtml.='</select>'; print '<form action="" method="post">'; print 'Cat: '.$cathtml; print 'Type: '.$typehtml; print '</form>';?>[/code] Link to comment https://forums.phpfreaks.com/topic/18321-creating-2-drop-down-menues-from-the-same-table/#findComment-78697 Share on other sites More sharing options...
Corona4456 Posted August 22, 2006 Share Posted August 22, 2006 Just combine both of your while loops into one. However, instead of echoing... create a string and just keep concatenating it as follows:[code]<?php echo '<form name="type1" method="post" action=""> <select name="type"> <option value="NULL" selected>- - - - - - - </option>'; $col1 = '<form name="type1" method="post" action=""> <select name="type"> <option value="NULL" selected>- - - - - - - </option>'; $col2 = '<form name="cat1" method="post" action=""> <select name="cat"> <option value="NULL" selected>- - - - - - - </option>'; while ($row = mysql_fetch_array($result)) { $col1 .= '<option value="'.$row['type'].'">'.$row['type'].'</option>'; $col2 .= '<option value="'.$row['cat'].'">'.$row['cat'].'</option>'; } $col1 .= '</select></form>'; $col2 .= '</select></form>'; // Now just echo them where ever you need them :)?>[/code]Crap... looks like someone beat me to it :) Link to comment https://forums.phpfreaks.com/topic/18321-creating-2-drop-down-menues-from-the-same-table/#findComment-78698 Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Share Posted August 22, 2006 Forgot to add that once you have added the submit button you would detect which was chosen by something like this:[code]if ($_POST['submit']) { $chosencat=$_POST['cat']; $chosentype=$_POST['type'];}[/code]This way you only need to create one form. Link to comment https://forums.phpfreaks.com/topic/18321-creating-2-drop-down-menues-from-the-same-table/#findComment-78700 Share on other sites More sharing options...
spires Posted August 22, 2006 Author Share Posted August 22, 2006 Thanks guy, i'm gonna go try them out. Link to comment https://forums.phpfreaks.com/topic/18321-creating-2-drop-down-menues-from-the-same-table/#findComment-78702 Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Share Posted August 22, 2006 Sorry for all the editing - I can't figure out why my posts come in boring grey whereas both of yours comes out boxed and in color :S Link to comment https://forums.phpfreaks.com/topic/18321-creating-2-drop-down-menues-from-the-same-table/#findComment-78703 Share on other sites More sharing options...
Corona4456 Posted August 22, 2006 Share Posted August 22, 2006 I believe it's the '<?php' tag.[code]<?php echo 'Hello World!'; ?>[/code] Link to comment https://forums.phpfreaks.com/topic/18321-creating-2-drop-down-menues-from-the-same-table/#findComment-78709 Share on other sites More sharing options...
Yesideez Posted August 22, 2006 Share Posted August 22, 2006 Yeah I just figured it out looking back at one of my other posts in anothr topic - thanks ;) Link to comment https://forums.phpfreaks.com/topic/18321-creating-2-drop-down-menues-from-the-same-table/#findComment-78710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.