Forum Moderators: coopster
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
<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']'
//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!
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>';
}
}
}