We help IT Professionals succeed at work.

How to filter product price range using checkbox?

Milind Bhuvad
on
374 Views
Last Modified: 2018-09-29
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 )
Comment
Watch Question

Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
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?
Co-Founder and Chief Architect
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Milind BhuvadPHP Developer

Author

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";
}
Dustin SaundersCo-Founder and Chief Architect
CERTIFIED EXPERT
Top Expert 2016

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.