Forum Moderators: coopster

Message Too Old, No Replies

Selective Selecting.

...using Checkboxes in search forms for filtering results.

         

inveni0

6:44 pm on Oct 5, 2006 (gmt 0)

10+ Year Member



I have a 'search' form that lets users search my database. I have a set of checkboxes in that form.

[]Area1
[]Area2
[]Area3

I'm having trouble figuring out how to filter my database query based on which boxes are checked.

Any ideas?

dreamcatcher

8:19 am on Oct 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi inveni0,

Ok, first you have your checkboxes and search box.


<input type="checkbox" name="search[]" value="Area1">
<input type="checkbox" name="search[]" value="Area2">
<input type="checkbox" name="search[]" value="Area3">
<input type="text" name="keywords">

Next we need to loop through the checkboxes. You can use a switch statement when you loop to determine your search row.


$query = '';
$search = $_POST['search'];
$keywords = $_POST['keywords'];

if (!empty($search))
{

for ($i=0; $i<count($search); $i++)
{

// Build switch to assign row name..
switch ($search[$i])
{
case 'Area1':
$row = row1';
break;

case 'Area2':
$row = 'row2';
break;

case 'Area3':
$row = 'row3';
break;
}

// Now build query..
if ($i)
{
$query .= "OR $row LIKE '%$keywords%' "'
}
else
{
$query = "WHERE $row LIKE '%$keywords%' ";
}

}

// Finally, query your db
mysql_query("SELECT * FROM table $query") or die(mysql_error());

}

Not tested, but something like that might give you a start.

dc

Birdman

7:29 pm on Oct 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We would really need to see your DB structure in order to give a more precise answer. Dreamcatcher's example looks sound but it may not work for you because we don't know exactly what's going on. For instance, does the keywords field exist in the search form.

inveni0

8:55 pm on Oct 6, 2006 (gmt 0)

10+ Year Member



There is no Keyword field, period. The search page looks like:

<input type= checkbox name="checkbox" value="A">Area 1
<input type= checkbox name="checkbox" value="B">Area 2
<input type= checkbox name="checkbox" value="C">Area 3
<input name="submit">

Or something like that.

The values of the checkboxes correspond to entries in columns of my database table. So, I can query like:

WHERE Area = 'A'

I just need it to work something like:

WHERE Area = '$_POST['checkbox']'

inveni0

1:39 pm on Oct 7, 2006 (gmt 0)

10+ Year Member



Actually, after implementing some other devices on the site, let's start over.

It would be nice if there was a way to show rows marked as, say, Area2 first, even though sorting by ASC or DESC would put Area2 in the middle.

eelixduppy

9:52 pm on Oct 8, 2006 (gmt 0)



How about this:

//connect to db server, select db
$query = "SELECT * FROM table";
$result = mysql_query($query);
$info = array();
while($row = mysql_fetch_array($result)) {
if($row['Area'] == $_POST['radio']) {
[url=http://us2.php.net/manual/en/function.array-unshift.php]array_unshift[/url]($info,$row);
} else {
[url=http://us2.php.net/manual/en/function.array-push.php]array_push[/url]($info,$row);
}
}
echo '<pre>';
print_r($info);
echo '</pre>';

* $_POST['radio'] cannot be an array, that's why I made it a radio instead of a checkbox. This is just an example, you may want to change it to fit your needs. *

This is off the top of my head. Good luck!

Birdman

12:44 pm on Oct 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a solution which also includes custom sorting. You just enter your preferred order in the $my_order array.

The Form:

<?php
// this catches error from an empty form submit
if (isset($_GET['error']) { print '<b>Please select an area</b>'; }
?>

<form method="post" action="/">
<input type="checkbox" name="area[]" value="A" />
<input type="checkbox" name="area[]" value="B" />
<input type="checkbox" name="area[]" value="C" />
<input type="checkbox" name="area[]" value="D" />
<input type="submit" name="submit" />
</form>

The Script:

if (isset($_POST['submit'])){

if(!isset($_POST['area'])) {
header('Location: /index.php?error=1');
}
$where = implode("' OR area = '", $_POST["area"]);
$sql .= "SELECT * FROM table WHERE area = '" . $where . "'";

// Now we'll get the results, build them
// into an array, then display them in your order

$arr = array();
$my_order = array('B', 'E', 'A', 'C', 'D');

$result = mysql_query($sql);

while ($i = mysql_fetch_array($result) {
$arr[ $i['area'] ] = $i;
}

foreach ($my_order as $sort) {

if ( isset($arr[$sort]) ) {

// Display your results here
// Note the array usage

print '<pre>';
print $arr[$sort]['area'] . '<br>';
print $arr[$sort]['another_mysql_column'] . '<br>';
print $arr[$sort]['yet_another_mysql_column'] . '<br>';
print '</pre>';

}

}

}