#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    30
    Rep Power
    17

    How to use selected data in the listbox for my query..


    Hi!

    I have a multiple select listbox and I pass the data using array in php. How can I use these records to my query, or in one select statement.

    For an example:

    I have a listbox of item codes; the user selects an item code/s to view it's inventory. The selected itemcodes will be pass on php using array and these records will be used as a query or search items on my database.

    Here is my code:

    My query:

    $result = mysql_query("select distinct itemcode from trn_detl where cust_code = '$custcode' order by itemcode");

    I populate my listbox with the result of the query above:

    echo "<SELECT name=\"invselect[]\" size=\"10\" multiple>\n";
    while ($row=mysql_fetch_assoc($result)) {
    echo "<OPTION value=\"{$row['itemcode']}\">{$row['itemcode']} </OPTION>\n";
    }
    echo "</SELECT>\n";

    So, what I want is that all entries that are selected on my listbox will be used as an argument in my query. For Example, option 2, 4, 5 are selected, I'll run a query to search for this itemcodes on my database and display it and it's corresponding quality. My proble is that can I use a single select to search for these?

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2001
    Location
    In a constant state of turmoil
    Posts
    855
    Rep Power
    87
    I'm not sure if I'm understanding your problem completely, but it sounds like you could accomplish this by using OR in your WHERE part of the sql statement.

    SELECT items FROM your_table WHERE item_code = 2 OR item_code = 4 OR item_code = 5
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    30
    Rep Power
    17
    Hi!

    Thanks for you reply.

    Yes, I can use the OR but the problem is the user can select unlimited number of itemcodes thus, I don't know how many will I include in the select OR statement.... I don't know also if I can put a loop within select code..

    btw, can I limit the number of selected items in a multiple select listbox? If this is possible, I can limit it to 10 item code and my query will be easy...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2001
    Location
    In a constant state of turmoil
    Posts
    855
    Rep Power
    87
    You can use a loop for your select statement like this:

    $sql = "SELECT * FROM your_table WHERE";

    PHP Code:
    if (sizeof($_POST['invselect']) > 0) {

      foreach(
    $_POST['invselect'] as $key => $value) {
        
        
    $sql .= " item_code = $value OR";
      }
    }

    $sql substr($sql0, -3); 
    That will fill in all the OR's in your select statement.

    This is off the top of my head and may need a little tweaking but it will work.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    30
    Rep Power
    17
    Wow!

    It works! Thanks very much! I really like it here! people are very helpful!!!

    Thanks again
  10. #6
  11. Wiking
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Sep 2000
    Location
    Sweden
    Posts
    3,608
    Rep Power
    31
    You could also do something like

    $query = "SELECT * FROM table WHERE something IN('" .join('\',\'', $array) ."')";

    //NoXcuz
    UN*X is sexy!
    who | grep -i blonde | date; cd ~; unzip; touch; strip; finger; mount; gasp; yes; uptime; umount; sleep

IMN logo majestic logo threadwatch logo seochat tools logo