Jump to content

Method to remove drop down options after being used


msaz87

Recommended Posts

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.

Link to comment
Share on other sites

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>';
   }
}

Link to comment
Share on other sites

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;

		?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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:

pc1.png

 

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.