msaz87 Posted November 21, 2009 Share Posted November 21, 2009 Hey all, I'm looking for a method of removing/hiding an option in a drop down after being used in a loop. The basic setup is it's being used as a schedule tool, with multiple game times and fields, each of which shows two drop downs that has every team in the league. So to prevent double-scheduling a team for one time, if the team was selected within that time's loop, it disappears out of the other drop downs. Hopefully that makes sense... any suggestions are appreciated! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/182387-method-to-remove-drop-down-options-after-being-used/ Share on other sites More sharing options...
cags Posted November 21, 2009 Share Posted November 21, 2009 Seeing the code your using to generate the dropdown (or the HTML if it's fixed) might have helped. But basically just have an array of used items, check each item as you output it to see if it's used. For example if it was for lets say Months. $months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); $used_months = array('March', 'May'); echo '<select name="month">'; echo '<option value="-1">Please Choose...</option>'; foreach($months as $month) { if(!in_array($month, $used_months)) { echo '<option value="' . $month . '">' . $month . '</option>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/182387-method-to-remove-drop-down-options-after-being-used/#findComment-962486 Share on other sites More sharing options...
msaz87 Posted November 22, 2009 Author Share Posted November 22, 2009 Seeing the code your using to generate the dropdown (or the HTML if it's fixed) might have helped. But basically just have an array of used items, check each item as you output it to see if it's used. For example if it was for lets say Months. $months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); $used_months = array('March', 'May'); echo '<select name="month">'; echo '<option value="-1">Please Choose...</option>'; foreach($months as $month) { if(!in_array($month, $used_months)) { echo '<option value="' . $month . '">' . $month . '</option>'; } } Here's the code that loops and sets up all the tables for the various times, their fields and then the different select areas.. Would your method still apply for this? Thanks for the help! for ($e = 0; $e < $times_rows; $e++) { $teams_1_list = mysql_query("SELECT * FROM tournament_teams WHERE tournament_id='$tournament' ORDER BY team ASC") or die(mysql_error()); $teams_2_list = mysql_query("SELECT * FROM tournament_teams WHERE tournament_id='$tournament' ORDER BY team ASC") or die(mysql_error()); $tables.= '<tr class="row'.$rowclass.'">'; $field_number = $field_no++; $tables.= '<td width="136">Field '.$field_number.'</td>'; ?> <input type="hidden" name="field[]" value="<? echo($field_number); ?>"> <? $tables.= '<td><center><select name="slot['.$field_number.'|'.$new_start_time.'|a]">'; $tables.= '<option>No Game</option>'; while($row = mysql_fetch_array($teams_1_list)){ $tables.= '<option value="'; $tables.= ($row['team_id']); $tables.= '">'; $tables.= ($row['team']); $tables.= '</option>'; } $tables.= '</select></center></td>'; $tables.= '<td><center>vs.</center></td>'; $tables.= '<td><center><select name="slot['.$field_number.'|'.$new_start_time.'|b]">'; $tables.= '<option>No Game</option>'; while($row = mysql_fetch_array($teams_2_list)){ $tables.= '<option value="'; $tables.= ($row['team_id']); $tables.= '">'; $tables.= ($row['team']); $tables.= '</option>'; } $tables.= '</select></center></td>'; $tables.= '</tr>'; $rowclass = 1 - $rowclass; } $field_no = 1; // reset field count $tables.= '</table></div></div></div><br/>'; } echo $tables; ?> Quote Link to comment https://forums.phpfreaks.com/topic/182387-method-to-remove-drop-down-options-after-being-used/#findComment-963059 Share on other sites More sharing options...
kickstart Posted November 22, 2009 Share Posted November 22, 2009 Hi Where do you store the already used items from the drop down list? If you have those stored in a table then you should be able to do a JOIN in the SQL to find the unused options (ie, do a LEFT OUTER JOIN against the table of used items, and only get those where the used record is a NULL. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/182387-method-to-remove-drop-down-options-after-being-used/#findComment-963127 Share on other sites More sharing options...
msaz87 Posted November 23, 2009 Author Share Posted November 23, 2009 Hi Where do you store the already used items from the drop down list? If you have those stored in a table then you should be able to do a JOIN in the SQL to find the unused options (ie, do a LEFT OUTER JOIN against the table of used items, and only get those where the used record is a NULL. All the best Keith The thing is, everything is being selected on the same page, so there's no reload or passing of variables to update what has/hasn't been used... To try and make it a little more clear, here's a photo of the selection area: So basically the way I'm hoping to have it done is that once a team is selected in the 9:45 AM games, it disappears for the rest of the dropdowns in that same time block, but remains in the others until it's selected there... So I'm guessing that would involve Javascript of some sort -- but I'm really clueless... Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/182387-method-to-remove-drop-down-options-after-being-used/#findComment-963838 Share on other sites More sharing options...
kickstart Posted November 23, 2009 Share Posted November 23, 2009 Hi Yep, think that would require javascript. Basically when you create the page load an array containing all the (I presume) teams. On a change of the drop down list remove the newly selected item from the array, and then use the array to rebuild the other lists. You will need to cope with adding teams back to the array / lists when someone changes their selection. Might be easiest to do it using Ajax. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/182387-method-to-remove-drop-down-options-after-being-used/#findComment-963846 Share on other sites More sharing options...
msaz87 Posted November 23, 2009 Author Share Posted November 23, 2009 Hi Yep, think that would require javascript. Basically when you create the page load an array containing all the (I presume) teams. On a change of the drop down list remove the newly selected item from the array, and then use the array to rebuild the other lists. You will need to cope with adding teams back to the array / lists when someone changes their selection. Might be easiest to do it using Ajax. All the best Keith This feature is more of a creature comfort than anything else... in your opinion is dealing with the javascript or ajax more trouble than it might be worth? I figure an alternate approach would be to add in a check system on the next page, basically to make sure there aren't any teams scheduled incorrectly and if there are, to bounce the user back before inserting it into the table... but obviously removing them immediately is more of a straightforward approach... Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/182387-method-to-remove-drop-down-options-after-being-used/#findComment-963853 Share on other sites More sharing options...
kickstart Posted November 23, 2009 Share Posted November 23, 2009 Hi If you have the time it is worth it. However possibly easier to just do a simple check in Javascript to flag up a message on screen if 2 drop downs contain the same value. Whatever you do you will need to recheck it on the server anyway. Someone could easily have javascript turned off. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/182387-method-to-remove-drop-down-options-after-being-used/#findComment-963854 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.