RE: [PHP-DB] multiple select statements
| From: | Shrock, Court | Date: | Tue, 05 Mar 2002 16:32:09 +0000 |
| Subject: | RE: [PHP-DB] multiple select statements | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-17474@lists.php.net to get a copy of this message | ||
Try creating your input with names that php will create arrays from, then
you can simply join the array elements if they are non-empty with something
like " OR $key='$val'".
An example:
<form>
<input type="text" name="filter[haircolor]">
<input type="checkbox" name="filter[registered]" value='Y'>
</form>
When submitted you could then:
$clause = '';
if (sizeof($_POST['filter']) > 0) {
$clause = 'WHERE ';
foreach($_POST['filter'] as $key=>$val) {
$tmp[] = "$key LIKE '%$val%'";
}
$clause .= join(' OR ', $tmp);
}
$sql = 'SELECT * FROM table $clause';
Of course you may want to add checks on what was entered or build a
mechanism so that you don't necessarily join all the filters with 'OR'. But,
I think you get the idea. Hope it helps.
Court
> -----Original Message-----
> From: Geoffrey Makstutis [mailto:gmak@gmak.net]
> Sent: Tuesday, March 05, 2002 8:12 AM
> To: php-db@lists.php.net
> Subject: [PHP-DB] multiple select statements
>
>
> Hi,
>
> I've got an HTML form which allows users to select various
> criteria to search for in my database (MySQL).
>
> The problem is that I can't seem to figure out how create the
> SELECT statement, given the fact that they could choose any
> or none of the criteria.
>
> Does anyone know of a way to dynamically create a SELECT
> statement that might have different numbers of criteria?
>
> Thanks
>
>