简体   繁体   中英

I want to fetch total number of likes against each post data using ajax and PHP

I want to fetch total number of likes on each post using ajax and PHP.

Here is my ajax.php file

<?php
include_once "../config.php";

 $user_id=$_POST["user_id"];
 $sale_phone_id=$_POST["sale_phone_id"];

$query2=mysqli_query($conn,"SELECT count(id) as total_likes FROM likes where status='1' AND sale_phones_id='$sale_phone_id'");

$row=mysqli_fetch_assoc($query2);
echo $row["total_likes"];
?>

Here is my JS

<script type="text/javascript">
           $(document).ready(function(){

                      var user_id=1;
                      var sale_phone_id=document.getElementById('demo_demo').getAttribute('data-value');
                      //var message=$("#message").val();

                      $.ajax({
                          type:"post",
                          url:"ajax.php",
                          data:"user_id="+user_id+"&sale_phone_id="+sale_phone_id,
                          success:function(data){

                            $("#thumbs_up").html(data);
                          }

                      });
            });
   </script>

The above mentioned code is only returning me the data of only first post. I want to show the number of likes against all posts.

Well you are getting only 1 record because you are sending just 1 sale_phone_id and your code is useful in a particular case where you need to find out the number of likes for a particular post.

From my understanding of your question, you need to count the number of likes on each and every sale phone. So have to put a join in your query (assuming sales_phone as your other table name); the query given below can fulfill your requirement

SELECT *, count(t2.id) as total_likes
FROM sale_phones t1 LEFT JOIN likes t2
ON t2.sale_phones_id=sale_phones_id
WHERE status='1' AND sale_phones_id='$sale_phone_id'

This way you will get all the sales_phones along with their number of likes.

Now once you get this result in JSON format (in response from your ajax call) the jquery logic is another question. Well it can be achieved in multiple ways. The simplest will be to use the id attribute with a prefix. Where looping through the sale_phones and creating posts you can assign each post a data id like

<?php foreach($sale_phones as $sp): ?>
    <div id="sp_<?php echo $sp->id; ?>" >
       <?php echo $sp->phone_name;?>
       <span class="likes"></span>
    </div>
<?php endforeach; ?>

and in your jquery ajax request function

$.ajax({
    type:"post",
    url:"ajax.php",
    success:function(data){
       // assuming that your data is returning in JSON format
       var response = JSON.parse(data);
       $('#sp_'+response.sales_phone_id).children('.likes').text(response.total_likes);
    }
 });

That's just a rough sketch to achieve what you need. Let me know if i can help out further.

By the way never send user_id from client side, it's highly insecure. User can change the ID by inspecting element or another way and will be able to see any user's records in response. Solution is to keep user_id in the session and take it from there whenever you need it in your PHP code instead of sending it from client side.

You're only fetching one row, you'll need to fetch all of them:

while (false != $row = mysqli_fetch_assoc($query2))
    echo $row["total_likes"];

you'll also need some way to seperate the values obviously...

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