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

    Join Date
    Mar 2013
    Rep Power

    Problem with searchform


    I have a search function where you can search for properties. Right now it only works if you fill in all the fields. But how would I do if I want to eg leave a field blank and just search the rest, or just search one field and leave the rest blank?

    This is the code for the query I have today:

    PHP Code:
    $sql mysql_query("
    SELECT * 
    FROM property
    WHERE (rooms LIKE '%
    $room%' OR '$room' = '')
    AND (status LIKE '%
    $status%' OR '$status' = '')
    AND (type LIKE '%
    $type%' OR '$type' = '')
    AND (adress LIKE '%
    $county%' OR '$county' = '')
    AND (area LIKE '%
    $area%' OR '$area' = '')
    AND (price BETWEEN '
    $min' AND '$max')
    ) or die(mysql_error()); 
    Thanks in advance!

    Maybe you should note that all the fields are select fields, not text fields.
  2. #2
  3. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    A Land Down Under
    Rep Power
    Just build the query using conditional statements,
    PHP Code:
    $args = array ();

    if (isset (
    $room) && strlen ($room) > 0) { // Set up your own conditions here
    $args [] = "(rooms LIKE '%".$room."'% OR rooms = '')"

    query "SELECT * FROM property WHERE ".implode (" AND "$args); 
    You can add in as many or as few if() statements as you need to.

    Basic reminder (just in case...): don't use any values in your query that have not been sanitised or escaped!!

IMN logo majestic logo threadwatch logo seochat tools logo