user1527354
user1527354

Reputation: 195

Making a smart notification list

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.

Upvotes: 2

Views: 2136

Answers (2)

Emir Akaydın
Emir Akaydın

Reputation: 5823

i believe the best solution is using GROUP BY in SQL not at PHP side. 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 show COUNT(*) as you intended.

EDIT: Examples.

SELECT event_type, COUNT(*) AS count FROM `notifications` WHERE `read` = '0' GROUP BY event_type

result:
event_type  count
----------  -----
4           4
6           2
9           1
12          2

Then you may run second queries for the events which are only appeared once.

SELECT * FROM `notifications` WHERE `read` = '0' AND `event_type` = '9'

of course you should also change their read status at the end

UPDATE `notifications` SET `read` = '1' WHERE `read` = '0'

These are just fast prepared examples. You should take care of special cases, updating only shown entries etc.

I used event_type as a field. I believe you have a field like this. You should rename that part according to your database structure.

Upvotes: 1

Ikstar
Ikstar

Reputation: 163

Taking an example from FB, the notifications are processed regardless of count. If we click on the number of likes on FB we get a kick back of who liked it, so the count is just a place holder for processed items.

When you are processing keep a count of notifications

mysql_query("SELECT * FROM `notifications` WHERE `read` = '0' AND 'date_added' > {SOMETIMEEXPRESSION} GROUP BY type LIMIT 20");
while($row = mysql_fetch_array($query)) {
if ($curType != $row['type']) {
    $curType = $row['type'];
    $curCount = 0;
    //Other Type switch conditions
 }
//    do the magic that you wish to do for the type.
}

Upvotes: 0

Related Questions