Jump to content

Remove selected item from dropdown list from query


raindrops87

Recommended Posts

hi i'm using this method for a dropdown list that i get from a table in mysql.

what i'm trying to do is to have a dropdown list and once i selected an item and press add it will be removed from the list but this only works for the first item i selected. when i try to select another item and press add both items will be back at the dropdown list.

 

the $_SESSION['CompanyAdded'] are the companies that i have selected and added.

 

can anybody help? thanks!

 

function contact()
{
  dbconnect();
  $result = mysql_query("SELECT CompanyName from customers");
  $array = $_SESSION['CompanyAdded'];
  echo "<td><select name=\"company\" id=\"company\">";
  
        while($row = mysql_fetch_assoc($result)){
            if($array != null){
                $item = true;
                for($i=0;$i<count($array);$i++){
                    
                    if($array != null){
                        if($row['CompanyName']!=$array[$i]){
                            $item = false;
                        }
                    }
                }
                if($item == false){
                     echo $row['CompanyName'];
                     echo "<option value=\"".$row['CompanyName']."\">".$row['CompanyName']."</option>";          
                }
                
            }else{
                echo $row['CompanyName'];
                echo "<option value=\"".$row['CompanyName']."\">".$row['CompanyName']."</option>";
            }
        }
  echo"</select></td>";

 

 

 

 

I would store those selected companies in the session then use a NOT IN clause to ensure they weren't returned in the SELECT form element

i.e.

  $array = $_SESSION['CompanyAdded'];
$companiesSelected = "('".implode("',",$array)."')";
  $result = mysql_query("SELECT CompanyName from customers WHERE CompanyName NOT IN $companiesSelected");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.