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>";

 

 

 

 

Link to comment
Share on other sites

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");

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.