Scenario
A image website that has basic features such as upload, comment, like, follow users etc.
Now i have a basic notification system that i built very quickly which just basically when a event happens such as an upload or like it is inserted into the notifications table, and then has a read column which determines whether the user has seen it or not, simple.
The current example output would be something like this,
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.
Which just basically is ordered by time of input.
Now what i would like to achieve is something similar to the following,
4 users liked Picture of my cat.
Henry Doe is now following you.
I am trying hard to wrap my average PHP knowledge around this and the best way i can think of is doing something like this.
$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']);
}
}
And then somehow sorting them to display correctly. But it still seems very complicated.
Also one more thing to think about if anyone comes up with a suggestion, would it be a good idea to have a algorithm in place that would group them even if they are not directly after each other, for example 5 minutes apart but with different notifications in between, for example
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.
Into
2 People liked Picture of my cat.
2 New Users are now following you.
i believe the best solution is using
GROUP BYinSQLnot atPHPside. You need to group the results by the event type. If there is a single result, just show the single result by using a second query. If there are more than one results, just showCOUNT(*)as you intended.EDIT: Examples.
Then you may run second queries for the events which are only appeared once.
of course you should also change their read status at the end
These are just fast prepared examples. You should take care of special cases, updating only shown entries etc.
I used
event_typeas a field. I believe you have a field like this. You should rename that part according to your database structure.