Shadowing Posted December 18, 2011 Share Posted December 18, 2011 trying to get my drop down box to display more then one result. I have two users with Mod wrote in the column Monitor but right now it only displays just the one. So I need to display all the goaulds that have mod in the monitor column <?php $result = mysql_query("SELECT goauld FROM users WHERE monitor ='mod' ORDER BY id DESC") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { if($row['goauld'] == $_POST['name_list']){ echo '<div class="containerc">'; echo '<div class="search">'; echo '<form method="post" >'; echo '<select name="name_list" class="textbox" id="name_list">'; echo "<option value=\"".$row['goauld']."\">".$row['goauld']."</option>"; echo "<option selected=\"selected\" value=\"".$row['goauld']."\">".$row['goauld']."</option>"; echo '</select>'; echo '<input type="submit" name="remove" id="remove" value="Remove">'; } } } '</form>'; '</div>'; '</div>'; ?> Quote Link to comment Share on other sites More sharing options...
dweeber Posted December 18, 2011 Share Posted December 18, 2011 First, <select> doesn't use selected. You normally identify what is selected by making that the first entry. You are better off collecting all of the values into an array so you can then use that to find what you need. Untested... <?php ... $result = mysql_query("SELECT goauld FROM users WHERE monitor ='mod' ORDER BY id DESC") or die(mysql_error()); $mods = array(); while($raw = mysql_fetch_array( $result )) { $mods[] = $raw; } echo '<div class="containerc"><div class="search"> <form method="post" > <select name="name_list" class="textbox" id="name_list">'; // First output the mod if they are in the post value and found in the data foreach($mods as $key => $value) { if ($value['goauld'] == $_POST['name_list'] ) { echo '<option value="' . $value['goauld'] . '"> ' . $value['goauld'] . '</options>'; } } // Next list the rest of them omitting the selected selected one foreach($mods as $key => $value) { if ($value['goauld'] != $_POST['name_list'] ) { echo '<option value="' . $value['goauld'] . '"> ' . $value['goauld'] . '</options>'; } } echo '</select> <input type="submit" name="remove" id="remove" value="Remove"> </form>'; Quote Link to comment Share on other sites More sharing options...
Shadowing Posted December 18, 2011 Author Share Posted December 18, 2011 thanks alot dweeber really appreciate it that works great i have a hard time with arrays but ive only been doing this for 3 weeks now lol. so hopefully i'll get to the point of comphrending them well. I like your quote btw This drop down box is to select someone to remove them and I notice when I hit remove it doesnt update the list. Anyway to fix that? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 18, 2011 Share Posted December 18, 2011 First, <select> doesn't use selected. Uh, yes it does. There's no need to go through all the backflips your code is doing to accomplish something so simple. <option value="value" selected="selected">label</option> Quote Link to comment Share on other sites More sharing options...
Shadowing Posted December 21, 2011 Author Share Posted December 21, 2011 whats a more simple way to do it? Quote Link to comment Share on other sites More sharing options...
Drummin Posted December 21, 2011 Share Posted December 21, 2011 foreach($mods as $key => $value) { if ($value['goauld'] == $_POST['name_list'] ) { echo '<option value="' . $value['goauld'] . '" selected="selected"> ' . $value['goauld'] . '</options>'; } else{ echo '<option value="' . $value['goauld'] . '"> ' . $value['goauld'] . '</options>'; } } Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 21, 2011 Share Posted December 21, 2011 foreach($mods as $key => $value) { echo '<option value="' . $value['goauld'] . '" ' . ($value['goauld'] == $_POST['name_list'] ? 'selected="selected"' : '') . '> ' . $value['goauld'] . '</options>'; } Simplified it. Quote Link to comment Share on other sites More sharing options...
Shadowing Posted December 31, 2011 Author Share Posted December 31, 2011 thanks alot drummin and scoot I know its been 9 days but i just now got the chance to try out your guys simplier way of doing it. Works perfectly and I understand it cause its much more simple. 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.