设想
具有上传、评论、点赞、关注用户等基本功能的图片网站。
现在我有一个基本的通知系统,我很快就建立了它,基本上当一个事件发生时,比如上传或喜欢它被插入到notifications
表中,然后有一个read
列来确定用户是否看到它,很简单。
当前的示例输出将是这样的,
John Doe liked Picture of my cat.
Sarah Doe liked Picture of my cat.
Adam Doe liked Picture of my cat.
Sarah Doe liked Picture of my cat.
Henry Doe is now following you.
这基本上是按输入时间排序的。
现在我想要实现的是类似于以下内容,
4 users liked Picture of my cat.
Henry Doe is now following you.
我正在努力将我的平均 PHP 知识包装起来,我能想到的最好的方法就是做这样的事情。
$query = mysql_query("SELECT * FROM `notifications` WHERE `read` = '0' LIMIT 20");
$likes = array();
$following = array();
while($row = mysql_fetch_array($query)){
if($row['type'] == "like"){
array_push($likes,$row['id']);
} elseif($row['type'] == "following"){
array_push($following,$row['id']);
}
}
然后以某种方式对它们进行排序以正确显示。但这似乎仍然非常复杂。
如果有人提出建议,还需要考虑一件事,如果有一个算法可以将它们分组,即使它们不是直接在彼此之后,例如相隔 5 分钟但有不同的通知,这是否是一个好主意介于两者之间,例如
John Doe liked Picture of my cat.
John Doe is now following you.
Sarah Doe liked Picture of my cat.
Sarah is now following you.
进入
2 People liked Picture of my cat.
2 New Users are now following you.