I want to link searches from a checkbox form together. E.g i have 5 checkboxes (a,b,c,d,e) if i have checked a and b, i want to search depending on what i have searched i.e display two different results.
<form role="form" action="R.php" method="post">
<div class="form-group">
<input type="checkbox" value="a" name="search" />a
<input type="checkbox" value="b" name="search" />b
<input type="checkbox" value="c" name="search" />c
<input type="checkbox" value="d" name="search" />d
<input type="checkbox" value="e" name="search" />e
</div>
<input class="btn btn-default glyphicon-align-right" type="submit" value=">>"/>
</form>
The PHP
<?php $output='';
if(isset($_POST['search'])){
$searchq=$_POST['search'];
$searchq = preg_replace ("#[^0-9a-z]#i","",$searchq);
$Squery = mysql_query("SELECT * FROM database WHERE Name LIKE '%$searchq%'");
$count = mysql_num_rows ($Squery);
echo "<div id=results><ul class=list-group><div id=qwerty>";
if($count == 0){
$output = 'There was no results!';
}else{
while($row = mysql_fetch_array($Squery)){
$name = $row ['Name'];
$id = $row['ID'];
$category = $row['Category'];
$output .='<div class="container">'.$name.$category.' '.$id.'</div>';
echo "<li>"."<div style=height:95px ><h3 class=text-center style=text-transform:capitalize >".$name.' '."</h3></div>".
"<div>"."<a href=n.php><img src=images/$id.jpg alt=Image /></a>
</div>".
' '.$category.' RESULT!!!!!'.$id."</li>";
}
echo "</div></ul></div>";
}
}
?>
If I understand the question correctly, you would want to be able to search for all results that match the checkboxes that you have checked.
I'm not sure exactly what results you would be looking for, so instead I'll give you a few different options and let you take it from there.
First, in order to pass MORE THAN ONE checkbox to your PHP script, you need to define you checkboxes differently.
<form role="form" action="R.php" method="post">
<div class="form-group">
<input type="checkbox" value="a" name="search[]" />a
<input type="checkbox" value="b" name="search[]" />b
<input type="checkbox" value="c" name="search[]" />c
<input type="checkbox" value="d" name="search[]" />d
<input type="checkbox" value="e" name="search[]" />e
</div>
<input class="btn btn-default glyphicon-align-right" type="submit" value=">>"/>
Notice the brackets after "search" .. name = "search[]". This allows your checkboxes to pass over a COLLECTION of values, instead of just one.
Secondly, on your PHP side.
Once you submit your checkboxes, you will want to get them the same way you currently do:
$searches = $_POST['search'];
$searches will now be an array filled with the values that were checked in your form.
Depending on your desired result, what you do next would look something like this.
Let's take the case that A and B were checked.
If you want to get all results where the word begins with the letter A OR the word begins with the letter B:
<?php
$searches = $_POST['searches'];
$Squery = "SELECT * FROM database";
$Swhere = "";
//add all your individual searches by looping through the checkbox values
foreach($searches as $search){
$Swhere .= " OR name LIKE '%$search' ";
}
//remove the first OR case, because it is not needed for the first search query
$Swhere = ltrim($Swhere, ' OR');
//add your dynamic where statement to your query
$Squery .= $Swhere
$result = mysql_query($Squery);
//...... run the query and get the results the same way you are
By using the foreach function, your can add all the checkboxes you want to your search, but the query on the PHP side will not have to change at all.
On a similar note, in the case of A selected and B selected, if you want to get all words that begin with A, begin with B, OR begin with AB, then your code would be adjusted like so:
<?php
$searches = $_POST['searches'];
//this takes your array of values and combines them into one word.
//your searches variable remains an array, but your stringSearch is now
//a word with the combined values
$stringSearch = implode($searches,'');
$Squery = "SELECT * FROM database";
$Swhere = " name LIKE '%$stringSearch%' ";
//add all your individual searches by looping through the checkbox values
foreach($searches as $search){
$Swhere .= " OR name LIKE '%$search' ";
}
//add your dynamic where statement to your query
$Squery .= $Swhere
$result = mysql_query($Squery);
//...... run the query and get the results the same way you are
Hopefully you can take this example and adjust to what your actual needs are.
One thing to note, is you SHOULD NOT be using mysql_query. It is opening your code up to major SQL injection concerns. I only used it above to display how you would do it with your current code. Look into the mysqli functions