5

I am getting particular list of product items through ajax, by passing their unique id to server. Now each product has its own set of properties which I have to display on page with product image. When I set the values through jquery, only last value in the array got printed. Following are my coding files.

images.php

while($fetch = mysql_fetch_array($result))
      {
      ?>

      <div class="col-sm-4">
       <div class="thumbnail">

        <a class="productitemid" href="productpurchase.php?id=<?php echo $fetch['itemID'];?>"><img class="img-responsive productimage" src="uploadedfiles\<?php echo $fetch['imageURL'];?>" alt="<?php echo $fetch['imageURL'];?>" /></a>

        <div class="text-center productitemname" style="font-weight:bold;"><?php echo $fetch['itemName']; ?></div>
        <div class="badge col-sm-offset-1 productprice"><?php echo $fetch['price']; ?></div>
        <span class="col-md-offset-7"><a class="productitemid btn btn-success" href="productpurchase.php?id=<?php echo $fetch['itemID'];?>">BUY</a></span>

       </div>
      </div>
      <?php
      }

js file

$(document).ready(function(){
  $('.menProdCatgry').on('click',function(){
   $.ajax({
    type: "post",
    url: "getselectedproducts.php",
    data:{
     "prodId" : $('.menProdCatgry').attr('prodCatId')
    },
    dataType: "json",
    success: function(data){
     console.log(data);
     $.each(data, function(){
     var getprodId = this.prodId;
     var getimageURL = this.imageURL;
     var getprice = this.price;
     var getitemName = this.itemName;
     var getitemID = this.itemID;

     $('.productimage').attr('src','uploadedfiles\/'+getimageURL);
     $('.productitemname').text(getitemName);
     $('.productprice').text(getprice);
     $('.productitemid').attr('href','productpurchase.php?id='+getitemID);

      });

    },
    error: function(data){
     console.log(data);
    }

   });
  });
 });
2
  • kindly please print the response when call the ajax console.log(data); Commented May 7, 2015 at 5:43
  • [Object, Object, Object, Object, Object, Object, Object] Object 0 Price: "800" imageURL: "a1.jpg" itemID: "55" itemName: "Printed Greyish Shirt" Similarly object 1, 2... Commented May 7, 2015 at 5:58

2 Answers 2

1

You can see the code of the foreach is only overwriting the values and attributes of the

 $('.productimage'),
 $('.productitemname') 
 // and so on

so you only see the last data of the response

$.each(data, function() {
            var getprodId = this.prodId;
            var getimageURL = this.imageURL;
            var getprice = this.price;
            var getitemName = this.itemName;
            var getitemID = this.itemID;

            // create a tag
            var a = $('<a/>');
                a.attr('href', 'productpurchase.php?id='+getitemID);
            // create new image
            var img = $('<img/>');
                img.attr('src', 'uploadedfiles/'+getimageURL);

            var prodname = $('<div/>')
                prodname.html(getitemName);

            var prodprice = $('<div/>');
                prodprice.html(getprice);
                // insert image to a
                a.append(img);

            var container = $('<div/>');
            // combine them all
            container.append(a);
            container.append(prodname);
            container.append(prodprice);
            // append to document
            // you can change this according to you need
            // to accomplish
            $('body').append(container);

        });

here i created a dynamic dom element for every iteration of the foreach then it will create a new sets of data then it will insert/include/append to the html element

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

Comments

0

An alternative solution..

If you added some sort of identifier for each product-block, like below:

<div class="thumbnail" id="prodId<?php echo $fetch['prodId'];?>">

You could narrow the selector in your each to a specific scope:

$.each(data, function(){
  var getprodId = this.prodId;
  var getimageURL = this.imageURL;
  var getprice = this.price;
  var getitemName = this.itemName;
  var getitemID = this.itemID;
  var myScope = '#prodId' + getprodId;

  $('.productimage', myScope).attr('src','uploadedfiles\/'+getimageURL);
  $('.productitemname', myScope).text(getitemName);
  $('.productprice', myScope).text(getprice);
  $('.productitemid', myScope).attr('href','productpurchase.php?id='+getitemID);
});

This will make sure that only classes found within your defined scope (#prodIdX) are selected and altered.

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.