-3

Ajax return only one product under range filter condition (products under price 68000) but actually database column price has more then one value under this range.

Here Database return result

public function Range(){
$conn=$this->makeConnection();
$query="select * from persons order by price desc";
$result = mysqli_query($this->conn,$query);
if(mysqli_num_rows($result) > 0){ 
     return $result;
     }else{
        echo "No results";
    }
}

Here i get the result in Html file

 <?php
    include("model/DALitem.php");
  $obj=new DALitem();
  $result=$obj->ViewData();
  $range=$obj->Range();
?>
  </div>
        <div align="center">
          <input type="range" min="0" max="100000" step="1000" value="1000" name="min_price" id="min_price">
          <span id="price_range"></span>
        </div>
        <br><br>
        <div class="row" id="product_loading">
          <?php 
            while ($row = mysqli_fetch_array($range)) {
             ?>
             <div class="col-3">
               <img <?php echo "src=\"assets/uploadedImages/".$row['file']."\""?> class="img img-fluid" style="height: 300px; width: 200px; background-color: teal">
                <h3><?php echo $row['name'] ?></h3>
               <h4>Price - <?php echo $row['price'] ?></h4>
                </div>
              <?php  
            }
          ?>
        </div>

Here is JQuery Script

    $(document).ready(function() {
   $('#min_price').change(function() {
    var price = $(this).val();
    $('#price_range').text('Product under price Rs. '+ price);
    $.ajax({
        url: 'load_product.php',
        type: 'POST',
        data: {price: price},
        success:function(data){
            $('#product_loading').html(data);
        console.log(data);
        }
    });
   });
  });

Here is load_product file to which Ajax interact

    <?php
include("model/config.php");
if (isset($_POST['price'])) {

    $output='';
    $query="select * from persons where price <=".$_POST['price']."";
    $result=mysqli_query($conn,$query);

    if(mysqli_num_rows($result) > 0){ 
        while ($row = mysqli_fetch_array($result)) {
             $output='
                <div class="col-3">
               <img src="assets/uploadedImages/'.$row['file'].'" class="img img-fluid" style="height: 300px; width: 200px; background-color: teal">
                <h3>'.$row['name'].'</h3>
               <h4>Price - '.$row['price'].'</h4>
                </div>
             ';
        }
    } else {
            $output='No Product Found';
            }
         echo $output;

}

?>

results under this code are It Only return one mobile but in price table all the products are under price range

Ali Raza
  • 673
  • 8
  • 19
  • 3
    You're overwriting the value of `$output` in every pass through the loop here: `while ($row = mysqli_fetch_array($result)) { $output='
    ` it should be `$output .= `, not `$output =`
    – Nick Apr 15 '19 at 09:17
  • thank you this works, but can you please explain this Nick. – Ali Raza Apr 15 '19 at 09:23
  • 1
    You need to concatenate all the `divs` into your output (using the `.=` operator), otherwise using `=` you will only get the last one as each new one will overwrite the last one. – Nick Apr 15 '19 at 09:24
  • Thank you very much brother, great help friend :) – Ali Raza Apr 15 '19 at 09:31

2 Answers2

1

I'd suggest you change your code a little bit instead of loading the whole .php file go for a JSON approach

So in your DB return

return json_encode($result);

In your jQuery you can use $.each function that jQuery offers

$.getJSON("url_with_json_here", function(data){
    $.each(data, function (index, value) {
        console.log(value);
    });
});

source: JQuery Parsing JSON array

Doc : http://api.jquery.com/jquery.each/

So once your set up your $.each you can use jQuery's .append() function

Doc: http://api.jquery.com/append/

So in the end your jQuery code should look a bit like this

$(document).ready(function() {
    $('#min_price').change(function() {

        $("#product_loading .col-md-3").remove();

        $.getJSON("url_with_json_here", function(data){
            $.each(data, function (index, value) {
                $("#product_loading").append(`
                    <div class="col-md-3">
                        `+ value  +`
                    </div>
                    `);
            });
        });     

    });
});
Frosty
  • 299
  • 5
  • 31
-2

Please check the changes, it should work now.

$output='';
$query="select * from persons where price <=".$_POST['price']."";
$result=mysqli_query($conn,$query);

if(mysqli_num_rows($result) > 0){ 
    while ($row = mysqli_fetch_array($result)) {
         $output .='
            <div class="col-3">
           <img src="assets/uploadedImages/'.$row['file'].'" class="img img-fluid" style="height: 300px; width: 200px; background-color: teal">
            <h3>'.$row['name'].'</h3>
           <h4>Price - '.$row['price'].'</h4>
            </div>
         ';
    }
} else {
        $output='No Product Found';
        }
     echo $output;} ?>
  • 3
    Please don't post answers to questions which are just simple typos. Instead, make a comment (as I have already done) and flag the question as "off-topic" due to a non-reproducible or typographical error. Questions like this add no value to the site and will eventually be deleted, thus causing you to lose any reputation that you may gain from answering. – Nick Apr 15 '19 at 09:28
  • 1
    @Nick - Unfortunately - Questions with accepted or upvoted answers won't be deleted. – Paul Spiegel Apr 15 '19 at 09:32
  • @PaulSpiegel it wasn't at the time... – Nick Apr 15 '19 at 09:40
  • @Nick - I know, I've seen that. But they can only "lose any reputation", if the answer is upvoted or accepted - And those won't be deleted. So nothing will stop this behavior, unless SO changes the "rules". – Paul Spiegel Apr 15 '19 at 09:48
  • @PaulSpiegel but they probably didn't know that until you pointed it out! :-) – Nick Apr 15 '19 at 09:58
  • @Nick I also thought that, but doubt, that they care. Note: I usually don't down-vote correct answers on OT questions. I down-voted this one, because it is missing any explanation. If you don't already know the answer, you would need a diff toll, to find the change. So for me it's more about quality. And I don't think it's a typo question - since the OP didn't even know the difference yet. So there might be a need to clarify the misunderstanding. But again - It's a low quality question, because of missing ***M**CVE*. – Paul Spiegel Apr 15 '19 at 10:10
  • @Nick that was not typos, i wasn't know why use the dot, you may be Best Developer but im not, im just a beginner or learner yet. – Ali Raza Jun 22 '19 at 09:11