我在php中创建了一个博客。用户可以查看他们朋友的帖子,他们可以“喜欢它”,就像你在facebook中一样。在每个帖子下都有一个链接,当用户按下它时,用户可以看到所有用户都喜欢这个特定的帖子。表评论包含所有帖子:评论(comments_id,评论,用户)和表赞(like_id,user,the_comment_id)包含每个帖子的所有喜欢。 我有以下问题。让我们说用户23,30喜欢先发帖子。用户30喜欢第二篇文章。如果我按下第一个帖子的链接以查看喜欢它的人,它会给我23和30.但如果我点击第二个帖子,它会给出23,30和30而不仅仅是它应该给我的30。 可能问题在于“同时”......任何想法如何解决这个问题?
这是我的PHP代码:
<?php
$sql = mysql_query("SELECT * FROM comments");
// starts a while loop that prints all posts...
while ($row= mysql_fetch_assoc($sql)) {
$comment_id = $row['comments_id'];
//... prints post details succesfully
$comments .= ".$row['comment'].";
// here starts the process for the link, that user will press it, it will get all users likes this certain post
$favorites = mysql_query(" SELECT * FROM `likes` WHERE `the_comment_id`='{$row['comments_id']}' ");
while($e = mysql_fetch_array($favorites)){
$like_users = $e['user'];
$array[]=$like_users;
}
$arstring = implode('</br>',$array);
$comments .= <<<EOD
// here starts link, that press and see all people like this comment
$arstring
EOD;
} //end while
答案 0 :(得分:0)
你应该将你喜欢的数组分开来进行评论。现在它是所有评论/帖子的单个数组。所以基本上只需在开始第二次while循环之前清除它。
你可以这样做:
<?php
$sql = mysql_query("SELECT * FROM comments");
// starts a while loop that prints all posts...
while ($row= mysql_fetch_assoc($sql)) {
$comment_id = $row['comments_id'];
//... prints post details succesfully
$comments .= ".$row['comment'].";
// here starts the process for the link, that user will press it, it will get all users likes this certain post
$favorites = mysql_query(" SELECT * FROM `likes` WHERE `the_comment_id`='{$row['comments_id']}' ");
$array = array(); // empty the list of any earlier users
while($e = mysql_fetch_array($favorites)){
$like_users = $e['user'];
$array[] = $like_users;
}
$arstring = implode('</br>',$array);
$comments .= <<<EOD
// here starts link, that press and see all people like this comment
$arstring
EOD;
} //end while