0

Basically what I am looking to do is populate a dropdown box from the contents of a mysql_array into a dropdown box...after the user makes a selection and submits the form the dopdown box will not contain the selection they just used....This form will have to be submitted mutiple times...I can get it to work no problem if it was just submitted once...so for multiple times i was trying to put what they had already used into an array and then compare that against the mysql_array so the already selected items wont appear again....this is what i have and i might be way off...

$_SESSION['name']=array();
array_push($_SESSION['name'],$_POST['selEmpl']);


function e(){
$sql="select name from IMSemploy ORDER BY name ASC"; 
$results=mysql_query($sql) or die(mysql_error()); 
echo"<select name=\"selEmpl\" size=\"1\">";
?><option value="Select">Select</option> <?
foreach ($_SESSION['name'] as $value){

while ($row = mysql_fetch_array($results)){

if($row['name']!=$value){

echo'<option value="'.$row['name'].'">'.$row['name'].'</option>';
}
}

}
echo'</select>';
}

2 Answers 2

1

So you want to hide the values that were already clicked?

You need to loop through the sql results set just once and don't display the values that are already stored in the session:

while ($row = mysql_fetch_array($results))
{
  if (array_search($row['name'], $_SESSION['name']) === false)
  {
    echo'<option value="'.$row['name'].'">'.$row['name'].'</option>';
  }
}

Apart from that you are destroying your $_SESSION['name'] variable every time you run the script, you need to set it to an empty array only the first time:

session_start();
if (!isset($_SESSION['name']))
{
  $_SESSION['name']=array();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Remove the foreach loop and test for this condition before displaying an option element

if (!in_array($row["name"], $_SESSION["name"])) {
    echo'<option value="'.$row['name'].'">'.$row['name'].'</option>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.