Re: Best Way
| From: | Koos van den Hout | Date: | Sun, 18 Feb 2001 10:59:45 +0000 |
| Subject: | Re: Best Way | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-6722@lists.php.net to get a copy of this message | ||
Quoting Don Smith who wrote on Sat, Feb 17, 2001 at 08:56:19PM -0600:
> I've searched the manual and read many examples, but I have to ask here for
> the best way to program for a number of possibilities. I've got my site
> working the hard way, but I'll bet there's a smarter way of doing the
> following:
>
> Visitors to my site can search my database by lastname and/or firstname
> and/or company. Is there a succinct way to checking for all possible
> combinations? I alreay have NONE SELECTED covered, but the rest of the
> possibilities are spotty. Too many IF statements, youknowwhaddimeanverne?
>
> SO... the user might just enter the last name, or just the first name, or
> just the first and last, or just the company or just the last name and the
> company.. and, oh well, you get the idea. Am I stuck with an awful lot of IF
> statements or is there a smarter way of handling this? The site is
> CameraCrews.com. Select "-by Name" to see the input page.
The smart way: building the query string on the fly. Something like:
$query="SELECT first,last,company FROM crews WHERE valid=1 ";
if ($q_firstname){
$query.="AND firstname like %$q_firstname% ";
}
if ($q_lastname){
$query.="AND lastname like %$q_lastname% ";
}
if ($q_company){
$query.="AND company like %$q_company% ";
}
$query.="ORDER BY entrydate";
$result=sybase_query($query);
Your query is a string too, use that fact :)
Koos
--
Koos van den Hout, PGP keyid RSA/1024 0xCA845CB5 via keyservers
koos@kzdoos.xs4all.nl or DSS/1024 0xF0D7C263 -?)
Fax +31-30-2817051 Visit my site about books with reviews /\\
http://idefix.net/~koos/ http://www.virtualbookcase.com/ _\_V