Is there anyone can tell me what's wrong with the following code? I am trying to do a search from a multiple select menu. It told me bad arguments with implode. Thanks!
In page1:

echo '<select name="test[]" multiple>';
echo '<option> ALL </option>';
echo '<option> t1 </option>';
....
echo '</select>';
.....

In page2:

....
$testId=$_POST['test[]'];
$q="SELECT ....FROM table WHERE";
$where ="....AND .... AND...";

if($testId!="ALL") {
$test_s=is_array($testId);
$testId=implode("," , $test_s);
$where.=" AND test='$testId'";
}
....
$sql=$q.$where;
$result=mysql_query(...);
....

Any help is appreciated.

    Your <OPTIONS> don't have value='s.

    $test_s is the boolean result of is_array(). implode() requires an array.

      you haven't specified values for your select options...

      echo '<select name="test[]" multiple>';
      echo '<option value="ALL"> ALL </option>';
      echo '<option value="t1"> t1 </option>';
      ....
      echo '</select>';

        Is there a way to read testId as array?

          and since $testId will now be a comma-delimited list, you will want to change your sql to:

          $where.=" AND test in ('$testId')";

          -> say the user selects both "all" and "t1", this will act as $TestId="all" OR $TestId="t1"

          This SQL will match any occurence of values selected in your multiple select form item.

          Your current sql will only match an exact combination of selected values if you user selects more than option

          ->basically saying $TestId must be equal to "all,t1". if you have "t1,all" stored in the database, it won't be matched.

            Just use implode before passing the variable to the next page:

            implode(seperator, string);

            and explode to turn it into an array:

            explode(seperator, string);

              barand is right. also, you cannot test for

              if($testId!="ALL")

              because of the same reason: testId is an array. you need to do you implode on $testId first, not $test_s, to convert the array to a list, and then parse the string to see if it contains "ALL". or you can loop over the and set a marker variable like $all=1 if the value "ALL" is found.

              then construct your sql, mindign what I told you about it higher up on the page.

                🙁 still not working yet.
                Does that
                $testId=$_POST['test[]'];
                look right?
                OR multiple selects store array as 2 dimensional in POST?

                😕

                  $testId = $_POST['test'];

                  $testId will always be an array in this case, even if only one value was selected from the list. To use it in your query, you need to implode and use IN (as Dniry said).

                  However, because they are text values, the need to be single-quoted, so you need to end up with ... IN ('All' , 't1' , 't2') so join() or implode() with quote-comma-quote as separator and put a quote at beginiing and end ...

                  $testId=$_POST['test']; 
                  $q="SELECT ....FROM table WHERE"; 
                  $testList = "'" . join("','" , $testId) . "'" ; 
                  $where.=" AND test IN ($testList)"; 
                  .... 
                  $sql=$q.$where; 
                  $result=mysql_query($sql)
                  
                    4 days later

                    Thanks, everyone.
                    Barand and Dniry's suggestion finally made multiple selection work. However, I have probelms with "ALL" option. The "ALL" option is not in my database. so it doesn't choose the whole data when $test IN 'ALL'.
                    The code so far is

                    $testId=$_POST['test']; 
                    $q="SELECT ....FROM table WHERE"; 
                    $where ="....AND .... AND...";
                    Above is for user choose "ALL" option.
                    
                    if($testId!="ALL") {
                    $testList = "'" . join("','" , $testId) . "'" ; 
                    $where.=" AND test IN ($testList)"; 
                    }
                    .... 
                    $sql=$q.$where; 
                    $result=mysql_query($sql);

                    Any ideas to fix this problem?
                    Thanks a lot.

                      If they select the ALL option, omit the WHERE clause.

                        yeah. that's what I did.
                        but it seems
                        if($testId!="ALL")
                        not work. it still gets to test IN 'ALL'.
                        😕

                          Try

                          $testId=$_POST['test'];  
                          $q="SELECT ....FROM table WHERE";
                          $testList = "'" . join("','" , $testId) . "'" ; if (strpos($testList, 'All') !== false) $where = "" else $where.=" AND test IN ($testList)";
                          ....
                          $sql=$q.$where;
                          $result=mysql_query($sql);

                          hth

                            🙁
                            same result. looks right. but still dosent' work.
                            🙁

                              oops. it works with

                              
                              if (strpos($testList, 'All') == false)

                              thanks barand!!!

                                Write a Reply...