Jump to content

multi select drop down/ append values to query


manouche

Recommended Posts

I have a form that uses a multi select dropdown. When submitted, I want to use those values to append to the query string. I already have the query string set up. For each value in the array, the appended query will be something like "AND color NOT LIKE '$c' "

 

I populate the drop down dynamically with a query to a database. So, say that the multi select looks like this:

 

<SELECT NAME ="color[]" multiple="multiple" SIZE="5">

<?php

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

 

{

echo "<option value= \"$row\">$row</option>";

 

}

?>

 

The code for the processing of the information looks like this:

 

if ($color) {

foreach ($color as $c) {

$mainquery = "color NOT LIKE '$c' ";

}

}

 

I can get one value of the array to append correctly, but that is all. I need it to somehow loop through the values of the array and append AND color NOT LIKE '$c'. So, ultimately, if red and blue were selected, it would say 'AND color NOT LIKE red and color NOT LIKE blue'.

 

Can someone point me in the right direction? Thank you.

What you want to do is loop through your array, and concatenate those values onto your query. (You were really REALLY close)

 

Change

   foreach ($color as $c) {
      $mainquery = "color NOT LIKE '$c' ";
      }

 

to

 

   foreach ($color as $c) {
      $mainquery .= "color NOT LIKE '$c' AND ";
      }

 

Notice the period before the equals sign, and the AND statement at the end of that.  What you want is

 

WHERE color NOT LIKE 'blue' AND COLOR NOT LIKE 'blue' AND COLOR NOT LIKE 'red'

 

Good luck!

 

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.