SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Nov 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with searchform

    Hello!

    I have a searchform 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
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    $valid 
    FALSE;
    $sql "SELECT *  
    FROM property 
    WHERE ("
    ;
    if(isset(
    $room)) { 
        
    $sql .=  " rooms LIKE '%$room%' AND ";
        
    $valid TRUE;
    }
    if(isset(
    $status)) { 
        
    $sql .=  " status LIKE '%$status%' AND ";
        
    $valid TRUE;
    }
    if(isset(
    $county)) { 
        
    $sql .=  " county LIKE '%$county%' AND ";
        
    $valid TRUE;
    }
    if(isset(
    $area)) { 
        
    $sql .=  " area LIKE '%$area%' AND ";
        
    $valid TRUE;
    }
    if(isset(
    $min) AND isset($max)) { 
        
    $sql .=  " price BETWEEN '$min' AND '$max' AND ";
        
    $valid TRUE;
    }
    if(
    $valid == FALSE) { die("bad query"); }
    $sql substr($sql0, -4);  // drop last AND
    $sql .= ")";

    $result mysql_query($sql) or die(mysql_error()); 

    ?>
    Last edited by captainccs; Mar 12, 2013 at 18:36. Reason: Remove a bug
    Denny Schlesinger
    web services


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •