searching with mutable criteria

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I am having a problem with this script what it does is write the sql
needed to search for data based on mutable criteria. It works fine if
you search with all the different fields but if you only search with
one or two of the fields it creates a sql that looks like this
SELECT * FROM listing WHERE state = 'WA' AND LIMIT 0,5
It adds the AND before the limit if you use all three fields it does
not add the AND. How can I change this so it will not add the AND if
there is only a search on one or two of the fields


$cond = "AND";
$sql = 'SELECT * FROM listing ';
if($_POST["state"] != "" || $_POST["types"] != "" || $_POST["county"] != ""){$sql .= "WHERE ";

if($_POST["state"] != ""){
$sql .= "state  = '". $_POST["state"] ."'";
if($_POST["state"] != "" || $_POST["types"] != "" || $_POST["county"] != ""){$sql .= " $cond ";}
}

if($_POST["type"] != ""){
$sql .= "types = '". $_POST["types"] ."'";
if($_POST["state"] != "" || $_POST["types"] != "" || $_POST["county"] != "") {$sql .= " $cond ";}
}

if($_POST["county"] != ""){
$sql .= "county = '". $_POST["county"] ."'";
if($_POST["state"] != "" || $_POST["types"] != "" || $_POST["county"] != ""){$sql.= " $cond "; }
}
)
$sql .= " LIMIT " . $from . "," . $max_results;
echo $sql
-- 
Best regards,
 Richard                          mailto:rdkurth@xxxxxxxxxxxxx

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux