How to filter product price range using checkbox?

I created product price filter using checkbox in php. I have 3 price range 2000-3000, 3000-4000, 4000-5000. If user click one checkbox its work, but user click more than one checkbox getting error (Notice: Trying to get property of non-object in D:\xamp\htdocs\aasopalav\controller\filter_saree.php on line 45).

here is my forntend code
<ul class="list-style-6 margin-50px-bottom text-small">
      <li><input type="checkbox" value="2000 AND 3000" class="item_filter price">2000 - 3000</li>
      <li><input type="checkbox" value="3000 AND 4000" class="item_filter price">3000 - 4000</li>
      <li><input type="checkbox" value="4000 AND 5000" class="item_filter price">4000 - 5000</li>
</ul>

and here is my back end code to filter
<?php
    $price="";
   $price = isset($_REQUEST['price'])?$_REQUEST['price']:"";

   $sql = "SELECT * FROM wm_products WHERE pro_live='N'";
            if(!empty($price)){
                  $price =implode("','",$price);
                  $sql  .= " and pro_price between $price";
            }

$result = $conn->query($sql);
      if($result->num_rows > 0){
            while($row = $result->fetch_assoc()){
                ?>
                       .........result which checkbox selected
                <?php
               }
        }
?>

In sort my select query created like this ( SELECT * FROM `wm_products` WHERE pro_live='N' AND pro_price BETWEEN 2000 AND 3000, 4000 AND 5000 )
Milind BhuvadPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
Hi Milind,

CheckBoxes allow you to check more than one.  Unless you take steps to manage what happens when more 1 are selected you can get odd behavior like you're seeing now.

Perhaps you want radio buttons, where only 1 button at a time can be selected?
0
Dustin SaundersDirector of OperationsCommented:
Your query has bad syntax:

AND pro_price BETWEEN 2000 AND 3000, 4000 AND 5000

Open in new window


If you're letting people select multiple boxes you can just append another OR clause rather than a comma separation.  You should wrap that in parenthesis.  So:

Examples:

AND (pro_price BETWEEN 2000 AND 3000
OR pro_price BETWEEN 4000 AND 5000)

Open in new window


AND (pro_price BETWEEN 2000 AND 3000
OR pro_price BETWEEN 3000 AND 5000
OR pro_price BETWEEN 4000 AND 5000)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Milind BhuvadPHP DeveloperAuthor Commented:
But how can implode checkbox value for between systax for this query

AND (pro_price BETWEEN 2000 AND 3000
OR pro_price BETWEEN 3000 AND 5000
OR pro_price BETWEEN 4000 AND 5000)

My query is

$sql = "SELECT * FROM wm_products WHERE ";
if(!empty($price)){
      $price =implode("','",$price);
      $sql  .= " and pro_price between $price";
}
0
Dustin SaundersDirector of OperationsCommented:
You would just need to look at all the check boxes, and run through a for loop.

If the box is checked, add to the conditions.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.