PHP select with several options

Discussion in 'PHP' started by gilgil2, Apr 14, 2012.

  1. #1
    Hi


    This is probably a fairly easily solved issue but I am struggling at the moment. I basically just want to be able to search a database but with several different options, any of which can be used and any combination can be used. The options will be name, date and location.


    What is the best way to do the search, I only want to search if something is entered into the form for that part so if no name is entered I do not want it to search for that etc.


    Another issue is the date and location are drop down menus, how do I use if(isset) for drop down menus, do I leave a black <select> option?


    Thanks in advance
     
    gilgil2, Apr 14, 2012 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    You need to do a var_dump of the $_GET or $_POST variables and see how the values that are selected are passed back to the server

    then build up your query as a string depending on the values passed.

    lets say you have one search field on your form and it can be matched to name, address and notes

    <?php
    $sql = "select * from mytable where ";
    $join = '';
    
    $fldSearch = '';
    if (isset($_POST['fldSearch'])) {
       $str = myDataSantize($_POST['fldSearch']); // do whatever you normally do to prevent sql injections etc
       $fldSearch = "(`name` like '%{$str}%' or `address` like '%{$str}%' or `notes` like  '%{$str}%')";
       $sql .= $join.$fldSearch;
       $join = ' AND ';
    }
    
    PHP:
    and so on until you've put in all your form fields
     
    sarahk, Apr 14, 2012 IP