RE: [PHP] MultiSelect List Box in PHP

From: Date: Sun, 31 Oct 2004 10:10:30 +0000
Subject: RE: [PHP] MultiSelect List Box in PHP
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-200754@lists.php.net to get a copy of this message
You can use this to work with. It's something I made some time ago, so it's probably not the most optimal solution, but it gets the job done... In this case it builds parameters for a SQL query, but it's easily modified to suit your needs. The reason it's a 'for' loop and not a 'foreach' is simply to make it simpler to keep track of how many items there IS in the list, so that the OR part of the SQL query gets built correctly.
    // load field list [multiselect box] from $_POST
    $fields = $_POST['fields'];
    // walk through all fields and build WHERE clause
    for ($i = 0; $i < count($fields); $i++) {
      if ($i > 0) {
        $addtoquery .= ' OR ';
      }
      switch ($fields[$i]) {
        case 'nick':
          $addtoquery .= "nickname LIKE '%$search%' OR akanick LIKE '%$search%'";
          break;
        case 'real':
          $addtoquery .= "fname LIKE '%$search%' OR lname LIKE '%$search%'";
          break;
        case 'email':
          $addtoquery .= "email LIKE '%$search%'";
          break;
        case 'icq':
          $addtoquery .= "icq LIKE '%$search%'";
          break;
        case 'aim':
          $addtoquery .= "aim LIKE '%$search%'";
          break;
        case 'msn':
          $addtoquery .= "msn LIKE '%$search%'";
          break;
        case 'yim':
          $addtoquery .= "yim LIKE '%$search%'";
          break;
      }
    } // end for - fields builder
The accompanying part of the form looks like this (don't ask why this is done with a for loop, guess we had about those when I did it):
      <td align="left" valign="top">Search in:<br>
        <select name="fields[]" id="fields" size="4" multiple>
          <?php
            $fieldarray = array('nick','real','email','icq','aim','msn','yim');
            $fieldnames = array('Nickname','Real name','Email','ICQ','AIM','MSN','YIM');
            for ($i=0; $i < count($fieldarray); $i++) {
              echo('<option value="'.$fieldarray[$i].'"');
              if (! empty($_POST['fields'])) {
                foreach($_POST['fields'] as $field) {
                  if ($fieldarray[$i] == $field) {
                    echo(' selected');
                  }
                }
              } else if ($fieldarray[$i] == 'nick') {
                echo(' selected');
              }
              echo('>'.$fieldnames[$i]);
            }
          ?>
        </select></td>
FWIW Rene At 09:21 31-10-2004, Andy B wrote:
Sorry that doesn't do me a huge bit of good since I have no clue how to write java in the first place. Does anybody know where I can find or get an example of php code that shows how to get all the selected items from a multiselect listbox and loop through them printing them on the screen??
-- Rene Brehmer aka Metalbunny If your life was a dream, would you wake up from a nightmare, dripping of sweat, hoping it was over? Or would you wake up happy and pleased, ready to take on the day with a smile? http://metalbunny.net/ References, tools, and other useful stuff... Check out the new Metalbunny forums at http://forums.metalbunny.net/

« previous php.general (#200754) next »