RE: [PHP] RE: (newbie) Multi filter table
| From: | Don Read | Date: | Mon, 21 Apr 2003 01:30:22 +0000 |
| Subject: | RE: [PHP] RE: (newbie) Multi filter table | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-144584@lists.php.net to get a copy of this message | ||
On 21-Apr-2003 Bobby Rahman wrote:
>
>
>
> Hiya
>
> I am sure that a few weeks a go I saw a thread regarding building a
> dynamic
> sql query. I have had a quick poke in the archives but cant locate it.
>
> Does anyone know how I can find out of filtering a table by using more
> than
> one filter selected from drop down boxes.
> e.g
> Table consists of all users
> SELECT * FROM users
> Filter selected from drop down box called Sex= Female
> I want the mysql to change to:
> SELECT * FROM users WHERE sex = Female
>
> Now say 2 filters are selected
> Sex= Female and work=Part-time
> I want the query to build to
> SELECT * FROM users WHERE sex=female and work=Part-time
>
> I think it can be done using a counter to add the equivalent WHERE and
> AND
> within query ...depending on the number of filters.
>
> Has anyone got a snippet of code that may help me, or point me to a
> tutorial/thread that I saw a few weeks ago.
>
Maybe mine (from the 16th) ?
function addclause($qry, $fld, $val) {
if (! empty($val) ) {
$qry .=sprintf(" %s $fld='%s'",
( preg_match('#\bwhere\b#mi', $qry) ? 'AND' : 'WHERE'),
$val);
}
return($qry);
}
$query="SELECT ... FROM da_table ";
if ($department != 'all')
$query=addclause($query, 'department', $department);
if ($location != 'all')
$query=addclause($query, 'location', $location);
Regards,
--
Don Read dread@texas.net
-- It's always darkest before the dawn. So if you are going to
steal the neighbor's newspaper, that's the time to do it.