简体   繁体   中英

Filter product using price in range

I am trying to make price filter for product, using checkbox. here is my filter

0-500
501-1000
1001-2500
2501-5000

If i select one checkbox then filter work but when i select more than one checkbox give me error like: Notice :

Trying to get property of non-object in D:\\xamp\\htdocs\\product\\filter_ledtv.php on line 11

Below i provide code what i tryed,
Here is my HTML Code

<div class="control-group">                     
<label class="control control--checkbox"> 0-500
  <input type="checkbox" value="0 AND 500" class="item_filter price"/>
  <div class="control__indicator"></div>
</label>                            
</div>
<div class="control-group">                     
<label class="control control--checkbox"> 501-1000
  <input type="checkbox" value="501 AND 1000" class="item_filter price"/>
  <div class="control__indicator"></div>
</label>                            
</div>
<div class="control-group">                     
<label class="control control--checkbox"> 1001-2500
  <input type="checkbox" value="1001 AND 2500" class="item_filter price"/>
  <div class="control__indicator"></div>
</label>                            
</div>
<div class="control-group">                     
<label class="control control--checkbox"> 2501-5000
  <input type="checkbox" value="2501 AND 5000" class="item_filter price"/>
  <div class="control__indicator"></div>
</label>                            
</div>

Here is my php code:

<?php
$price="";
$price = isset($_REQUEST['price'])?$_REQUEST['price']:"";

$sql = "SELECT * FROM wm_ledtv WHERE pro_live='N'";
if(!empty($price)){
    $price =implode("'or pro_price between'",$price);
    $sql  .= " and pro_price between $price"; 
}
$result = $conn->query($sql);
if($result->num_rows > 0){
    while($row = $result->fetch_assoc()){
        ?>
         fliter product show.............
        <?php
    }
}
?>

A few problems.

First, you don't have name="price[]" in your checkbox inputs.

Second, you're adding inappropriate quotes around the price ranges. It should be:

$price =implode(" or pro_price between ",$price);

If you did echo $sql; you would have seen that the query looked wrong, you'd see:

WHERE pro_price BETWEEN 0 AND 500'or pro_price between'501 AND 1000

Your code is also vulnerable to SQL-injection. It would be best if you used parametrized queries, but you should at least sanitize the inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM