1

I am filtering the results from the database using checkboxes and it works fine with one checkbox however when I check multiple checkboxes it returns the rows only for the first checkbox. How can I have it to filter the results with multiple checkboxes?

Here are the checkboxes

 <form id="form" method="post" action="">
<input type="checkbox" name="football" class="checkbox" <?=(isset($_POST['football'])?' checked':'')?>/> Football<br>
<input type="checkbox" name="basketball" class="checkbox" <?=(isset($_POST['basketball'])?' checked':'')?>/> Basketball<br>
<input type="checkbox" name="volleyball" class="checkbox" <?=(isset($_POST['volleyball'])?' checked':'')?>/> Volley Ball<br>
 </form>

 <script type="text/javascript">  
    $(function(){
     $('.checkbox').on('change',function(){
        $('#form').submit();
        });
    });
</script>

here is the select query.. the third else if statement returns only the first checkbox value which is basketball

if (isset($_POST["football"])){
  $paginate = new pagination($page, "SELECT * FROM balls where type LIKE '%foot%' ORDER BY id desc", $options);
 }

else if (isset($_POST["basketball"])){
  $paginate = new pagination($page, "SELECT * FROM balls where type LIKE '%bask%'   ORDER BY id desc", $options);
 }

else if (isset($_POST["volleyball"])){
  $paginate = new pagination($page, "SELECT * FROM balls where type LIKE '%vol%'  ORDER BY id desc", $options);
 }

else if (isset($_POST["basketball"]) && isset($_POST["football"]) && isset($_POST["volleyball"])){
  $paginate = new pagination($page, "SELECT * FROM balls where type LIKE '%bask%' or type LIKE '%foot%' or type LIKE '%vol%' ORDER BY id desc", $options);
 }

else { $paginate = new pagination($page, 'SELECT * FROM balls ORDER BY id desc', $options);

}

2 Answers 2

3

If football is set your page won't call any of the other functions. You could fix it by putting the if (isset($_POST["basketball"]) && isset($_POST["football"]) && isset($_POST["volleyball"])){ as the first argument instead of the third, but this method doesn't handle cases where only two of the three are selected.

EDIT 1: A more appropriate method would be to have the following:

if (isset($_POST["basketball"]) {
  $arguments[] = "type LIKE '%bask%'";
}
if (isset($_POST["football"]) {
  $arguments[] = "type LIKE '%foot%'";
}
if (isset($_POST["volleyball"]) {
  $arguments[] = "type LIKE '%vol%'";
}
if(!empty($arguments)) {
  $str = implode(' or ',$arguments);

  $qry = "SELECT * FROM balls where " . $str . " ORDER BY id desc";

  $paginate = new pagination($page, $qry, $options);
} else {
  //Whatever happens when there's none checked.
}

EDIT 2: Let me explain what's going on here. First we check each post variable to see if its set. If it is, we add a value to the $arguments array with the type LIKE '%search term%' argument.

Then once we've checked all of our arguments, we implode them together and delimit by " or ". We then plug this argument string into our query and run a single pagination with exactly what we need. No more, no less.

Sign up to request clarification or add additional context in comments.

12 Comments

yep that is also a prob. what would be better approach for handling such selection?
I updated my answer with an example of how you could deal with multiple scenarios.
this gives me Parse error: syntax error, unexpected '$paginate'
Oh sorry, my bad. Add a semicolon after the $qry = "SELECT * FROM balls where " . $str . " ORDER BY id desc" I'll edit my answer to include it.
also does this return all the rows from the table if none of the checkboxes are checked?
|
0

You use a sequence of else if blocks. Therefore, only one will be evaluated.

Change this and you will be fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.