Я хочу показывать лайки в посте, на котором нажата кнопка лайка. Не в первом извлеченном посте. Пожалуйста, помогите мне в этом вопросе. Код ответа ajax и извлеченная часть постов, страница ответа функции ajax.
примерочных post.php
<div class='row post_all'>
<?php
$query_tp=mysqli_query($connect,"SELECT * FROM parent_post"enter code here);
while($data_tp=mysqli_fetch_object($query_tp)){
$query_by=mysqli_query($connect,"SELECT * FROM register_1 WHERE register_id='".$data_tp->register_1_id."'");
while($data_posted_by=mysqli_fetch_object($query_by)){
$query_p_likes=mysqli_query($connect,"SELECT * FROM likes WHERE posts_id='".$data_tp->post_id."'");
$total_likes=mysqli_num_rows($query_p_likes);
$query_p_mood=mysqli_query($connect,"SELECT * FROM parent_post_mood WHERE pp_mood_id='".$data_tp->mood_fk."'");
while($data_p_mood=mysqli_fetch_assoc($query_p_mood)){?>
<div class='col-lg-7'>
<div class="thumbnail" style="background-color:#F5F5F5;">
<img src="image_convert/try_post_pic.php?tp_pic=<?php echo $data_tp->post_id;?>" class="img-rounded" alt="Picture here" style="width:100%;height:300px;cursor:pointer;" title="<?php for($i=1;$i<=$counter;$i++){ echo $data_p_mood['fk'.$i]." ";}?>">
<div class="caption">
<div class='row'>
<div class='col-lg-12' style="border-bottom:2px solid #FFFFFF">
<h7 style="color:#642C37; font-weight:bold; font-size:20px;"><?php echo $data_tp->post_title;?></h7>
</br>
<p style="font-size:16px; color:#000; text-align:left;"><?php echo $data_tp->post_description;?></p>
</div><!--col-lg-12-->
<div class='row' style="padding:0 0 0 5px;">
<form>
<input type="hidden" id='like_p' value="<?php echo $total_likes;?>">
<input type="hidden" id='pos_id' value="<?php echo $data_tp->post_id;?>">
<input type="hidden" id='username' value="<?php echo $_SESSION['un'];?>">
<input type="hidden" id='userid' value="<?php echo $_SESSION['u_id'];?>">
</form>
<div class='col-lg-1'>
<p><span class='glyphicon glyphicon-thumbs-up' id='like_btn' onClick="add_like(<?php echo $data_tp->post_id;?>,<?php echo $data_posted_by->register_id;?>,<?php echo $total_likes;?>)" style="color:#2986D8;font-size:22px;font-weight:bold;cursor:pointer;"></span><span class='badge' id="like_bg"></span></p>
</div><!--col-lg-4-->
<div class='col-lg-2' style="font-size:20px;">
<p onClick="comment_p(this,<?php echo $data_tp->post_id;?>)" accesskey='<?php echo $data_tp->post_id;?>' style='cursor:pointer;'>Comment</p>
</div><!--col-lg-4-->
<div class='col-lg-2' style="font-size:20px;">
<a href="#">Share</a>
</div><!--col-lg-4-->
<div class='col-lg-7' style="padding:6px 0 0 0;"><p style="font-size:14px;">Posted by <i><a href="#"><?php echo ucwords($data_posted_by->f_name)." ".ucwords($data_posted_by->l_name);?></a></i><?php echo " (".$data_tp->post_date.")";?></p></div><!--col-lg-6-->
</div><!--row-->
<div id="comment_row"></div><!--comment_row-->
</div><!--row-->
</div><!--caption-->
</div><!--thumbnail-->
</div><!--col-lg-8-->
</hr>
<?php }/*while most inner*/?>
<?php }/*while inner*/?>
<?php }/*while*/?>
</div><!--row-->
Функция AJAX
<script>
function add_like(p_id,user_id,likes){var val2=$("#username").val();
var val3=$("#userid").val();
//alert(p_id+' '+user_id+' '+val2+' '+val3);
$.get("ajax/like_add.php",{p_id:p_id,user_id:user_id,val2:val2,val3:val3,likes:likes},function(data){$("#like_bg").html(data);
});}/*getRec*/
like_add.php
<?php include_once("../connect/dbconnection.php");?>
<?php
$parent_post_id=$_GET['p_id'];
$user_id=$_GET['user_id'];
$sess_uname=$_GET['val2'];
$sess_id=$_GET['val3'];
$lik=$_GET['likes'];
if(!empty($sess_id)){
$query_like_check=mysqli_query($connect,"SELECT * FROM likes WHERE posts_id='$parent_post_id' AND register_1_id='$sess_id'");
$likes_rec=mysqli_num_rows($query_like_check);
if($likes_rec==0){
$query_likes=mysqli_query($connect,"INSERT INTO likes(posts_id,register_1_id)VALUES('$parent_post_id','$sess_id')");
echo "(".$lik.") Like";
//echo "<h3>".$parent_post_id.$user_id.$sess_uname.$sess_id."</h3>";
}/*to check if the user have already liked the post*/
else{
$query_likes_del=mysqli_query($connect,"DELETE FROM likes WHERE posts_id='$parent_post_id' AND register_1_id='$sess_id'");
$query_again_like=mysqli_query($connect,"SELECT * FROM likes WHERE posts_id='$parent_post_id' AND register_1_id='$sess_id'");
$q_a_l=mysqli_num_rows($query_again_like);
echo "(".$q_a_l.") Like";
}/*else to check if the user have already liked the post*/
}/*if session id*/
else{
echo "Unauthourization of user";
}/*else session id*/
?>
Я бы предложил использовать INNER JOIN в одном запросе вместо использования while in while и просто использовать многомерные массивы. Ох, а также использовать метод POST при отправке форм. P.S. Ваш сайт сейчас очень уязвим для SQL-инъекций.
Других решений пока нет …