Re: search by multiple varaibles...
| From: | Robin Vickery | Date: | Thu, 31 Aug 2000 09:14:45 +0000 |
| Subject: | Re: search by multiple varaibles... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-14550@lists.php.net to get a copy of this message | ||
webshot@neoncowboy.com ("John Corry") writes:
> So far, I have a form that can be used to search a database by three
> separate fields.
>
> //relevant generation of query
> //base query
> $searchStmt = "SELECT *, hi_price, lo_price from $table where " ;
>
> //plus concatenated query
> if ($price_key)
> $searchStmt .= "price_key like '$price_key' and " ;
> if ($title)
> $searchStmt .= "title like '$title' and " ;
> if ($area)
> $searchStmt .= "area like '$area' and " ;
>
> //minus the trailing 'and '
> $stmt= substr($searchStmt, 0, strlen($searchStmt)-4) ;
>
> But what I really need is to be able to search a broader section of the DB.
> This works great if the user only selects one variable for each search
> term...but how do I write it if they need to be able to select *multiple*
> variables?
<?php
$title = array( "Driftglass", "Time Considered as a Helix of Semi-Precious
Stones" );
$area = array( "Devon", "Cornwall", "Somerset" );
$searchStmt = "SELECT *, hi_price, lo_price from $table where ";
$querypart = array();
if (sizeof($title)) { $querypart[] = "(title like '" . join("' or
title like '", $title) . "')"; }
if (sizeof($price_key)) { $querypart[] = "(price_key like '" . join("' or
price_key like '", $price_key) . "')"; }
if (sizeof($area)) { $querypart[] = "(area like '" . join("' or
area like '", $area) . "')"; }
$searchStmt .= join(" and ", $querypart);
print $searchStmt;
?>
--
Robin Vickery...............................................
Planet-Three, 3A West Point, Warple Way, London, W3 0RG, UK
Email: robin@planet-three.net Phone: +44 (0)870 729 5444