php mysql“;“喜欢按钮”;通知
编写php/mysql代码将与某篇文章相关的所有“喜欢”分组,甚至显示单击“喜欢”按钮的人的最短方法是什么,如以下示例所示:php mysql“;“喜欢按钮”;通知,php,mysql,Php,Mysql,编写php/mysql代码将与某篇文章相关的所有“喜欢”分组,甚至显示单击“喜欢”按钮的人的最短方法是什么,如以下示例所示: John、Mary、Brian和其他人喜欢这条评论因为您没有给出任何其他详细信息,除了您想要的内容之外,这都是基于您的数据库可能如何设置的假设 SQL查询: SELECT * FROM likes WHERE (comment or post id) = 'ID' post_id是您希望分组的对象,因此,例如,如果每个评论都有自己的id,那么您希望根据该id对您喜欢的
John、Mary、Brian和其他人喜欢这条评论因为您没有给出任何其他详细信息,除了您想要的内容之外,这都是基于您的数据库可能如何设置的假设 SQL查询:
SELECT * FROM likes WHERE (comment or post id) = 'ID'
post_id是您希望分组的对象,因此,例如,如果每个评论都有自己的id,那么您希望根据该id对您喜欢的内容进行分组
我喜欢的数据库设置如下:
$num = 0; // This is used as an identifier
$numrows = mysqli_num_rows($getdata); // Count the number of likes on your comment or post
if($numrows > 3) {
$ending = 'and others like this comment.'; // If there are more than 3 likes, it ends this way
} else {
$ending = 'like this comment.'; // If there are less than or equal to 3 likes, it will end this way
}
while($data = mysqli_fetch_array($getdata)) {
if($num => 3) { // This says that if the $num is less than or equal to 3, do the following
// This will be used to list the first 3 names from your database and put them in a string like: name1, name2, name3,
$names = $data['name'].', ';
// This adds a number to the $num variable.
$num++;
}
}
echo $names.' '.$ending; //Finally, echo the result, in this case, it will be: John, Mary, Ryan, and other like this comment.
字段:id、注释id、帖子id、名称
所以你会有这样的想法:
+--------------+---------------+--------------+--------------+
| ID | comment_id | post_id | name |
+--------------+---------------+--------------+--------------+
| 1 | 382 | null | John |
| 2 | 382 | null | Mary |
| 3 | null | 189 | Brian |
| 4 | null | 189 | Joe |
| 5 | 382 | null | Ryan |
| 6 | 382 | null | Bell |
+--------------+---------------+--------------+--------------+
因此,如果使用SQL脚本:
SElECT * FROM likes WHERE comment_id = '382'
您将获得以下信息:
+--------------+---------------+--------------+--------------+
| ID | comment_id | post_id | name |
+--------------+---------------+--------------+--------------+
| 1 | 382 | null | John |
| 2 | 382 | null | Mary |
| 5 | 382 | null | Ryan |
| 6 | 382 | null | Bell |
+--------------+---------------+--------------+--------------+
然后运行一个脚本(假设它是PHP),如下所示:
$num = 0; // This is used as an identifier
$numrows = mysqli_num_rows($getdata); // Count the number of likes on your comment or post
if($numrows > 3) {
$ending = 'and others like this comment.'; // If there are more than 3 likes, it ends this way
} else {
$ending = 'like this comment.'; // If there are less than or equal to 3 likes, it will end this way
}
while($data = mysqli_fetch_array($getdata)) {
if($num => 3) { // This says that if the $num is less than or equal to 3, do the following
// This will be used to list the first 3 names from your database and put them in a string like: name1, name2, name3,
$names = $data['name'].', ';
// This adds a number to the $num variable.
$num++;
}
}
echo $names.' '.$ending; //Finally, echo the result, in this case, it will be: John, Mary, Ryan, and other like this comment.
SELECT*FROM likes WHERE postd=?
?假设您在一个页面上有20个“like按钮”,用于不同的信息。每个按钮都有1000人点击。“SELECT*FROM likes WHERE postId=?”会不会变得有点慢和不够?它会变得非常慢,但你要求的是最短的路。@Boann你是对的,我的错。哇!谢谢你,布莱恩!你真的读懂了我的心思!这正是我需要的!让我测试一下代码,然后再给你回复!