Forum Moderators: coopster

Message Too Old, No Replies

Simple MySQL query build - checkboxes & OR statements

Simple MySQL query build - checkboxes & OR statements

         

HotShot1

7:35 am on May 8, 2005 (gmt 0)



I have simple (yes, I know) MySQL qry to build,
it has 30 fields that COULD have been posted:
'f1' to 'f29' are checkboxes (any single or multiple could be checked and one text 'searchCriteria'.

The problem is that now (after posting I need to build the qry and I need a loop, but I can't find a loop that will manage the "OR" statements.
i.e.


$query = "SELECT * FROM `db1`.`tbl1`
WHERE
`f1` LIKE '%$searchCriteria%'
OR `f2` LIKE '%$searchCriteria%'
OR `f20` LIKE '%$searchCriteria%'
OR `f29` LIKE '%$searchCriteria%'
LIMIT 0 , 999";

Can anyone point my dumb a** in the right direction?

Any help Appreciated,
HotShot1

dreamcatcher

12:23 pm on May 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Hotshot1, welcome to Webmaster World.

Best to have your checkboxes as an array, then loop through them. Like this:

<input type="checkbox" name="search[]">

for ($i=0; $i<count($_POST['search']); $i++)
{
if ($i)
{
$sql .= "OR somefield LIKE '%$searchCriteria%' ";
}
else
{
$sql = "WHERE somefield LIKE '%$searchCriteria%' ";
}
}

Then use $sql in your query.

$query = "SELECT * FROM `db1`.`tbl1` $sql";

Hope that gives you a starting point.

dc