Jump to content

Remove Selected Items


euel

Recommended Posts

Hi guys!

I'm trying to make a <select> button for car positions which displays a set of numbers like 1-5 or 1-10 so on..

I need to remove the numbers that is already been selected.

Selected numbers are stored in the database and i fetch them in a simple query then compared it to the max number of cars.

 

I have this code, it kinda works but it shows the number 1 which is already been selected.

Selected numbers are 1 and 3, it successfully removes number 3 but not 1,

The output is 1,2,4,5,6,7,8,9,10

I added some comments for clarity..

 

      function position_available($event_id, $number_of_cars)    // receives id for ref. in the database
{
$selected_positions = check_selected_positions($event_id);   // query to get selected positions
while($positions = mysql_fetch_array($selected_positions))
{
	$position = array();
	array_push($position, $positions['car_position']);  //  'car position' is the name of the field

}

	for($i = 1; $i <= $number_of_cars; $i++)     // $number_of_cars is the total number of cars
	{
		if(! in_array($i, $position))
    		echo "<option value='{$i}'>" . $i . "</option>";

	}

}

 

Did i miss something simple?

Thx for the help in advance!

Link to comment
Share on other sites

while($positions = mysql_fetch_array($selected_positions))
{
	$position = array();
	array_push($position, $positions['car_position']);  //  'car position' is the name of the field

}

you are defining the position array within the while loop.  that meens that you are reseting the array each time you iterate through the loop.  It's what's causing your problem as it will (best case) only hold the last record parsed by the loop.  move the assignment up above the beggining of the loop and you should be fine.

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.