I have a social networking site where I just started logging certain user actions to a table.
Actions such as when they update their mood, edit their profile and update new photos.
I want to integrate a sort of news feed to a user's profile where it will show these updates for users they've added as their favorites.
The issue I'm running into is that if a user updates their profile four times in a half an hour, I'd like to only show that as one update.
Same goes for photos. If a user adds four photos within an hour, I want that to group that as one item.
The structure for the update log (update_log) is as follows:
update_id (auto)
userid INT
update_type (1=profile update, 2=photo added, 3=mood updated)
pictureid INT (joins to userphotos table with file information)
mood_a INT (joins to lookuptable that holds first part of selectable mood)
mood_b INT (joins to lookuptable that holds second part of selectable mood)
mood_c INT (joins to lookuptable that holds third part of selectable mood)
update_time INT
Typical records for the three scenarios are as follows:
Profile update:
(auto), 1, 1, 0, 0, 0, 0, 1238598393
Photo added:
(auto), 1, 2, 10000, 0, 0, 0, 1238598393
Mood updated:
(auto), 1, 3, 0, 5, 10, 15, 1238598393
I'm pretty sure this can't be done with a query as I need the individual photo file information to display thumbnails.
What's the best way to organize and sort through this information in PHP?
My brain is failing me on this.
Below is the MySQL query I wrote to grab all the pertinent data I need to display this feed for updates in the past 48 hours and attached to this post is some sample data returned from that query,
SELECT l.update_id,
l.userid,
l.update_type,
t.description,
p.filename,
l.mood_a,
a.description AS mood_a_description,
l.mood_b,
b.description AS mood_b_description,
l.mood_c,
c.description AS mood_c_description,
l.update_time
FROM update_log l
JOIN update_types t
ON l.update_type = t.code
LEFT JOIN userphotos p
ON l.pictureid = p.pictureid
LEFT JOIN lookuptables a
ON l.mood_a = a.code AND
a.tableid = 'mood_a'
LEFT JOIN lookuptables b
ON l.mood_b = b.code AND
b.tableid = 'mood_b'
LEFT JOIN lookuptables c
ON l.mood_c = c.code AND
c.tableid = 'mood_c'
WHERE FROM_UNIXTIME(update_time) > now() - INTERVAL 48 HOUR
ORDER BY l.update_time DESC
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
Select allOpen in new window
by: bansidharPosted on 2009-04-07 at 23:27:01ID: 24094375
Here is some Idea you can use.
esult)){ _hr][] = $row;
Use
ORDER BY update_type.update_time DESC
here you will get all the records ordered by the type first and inside that ordered by time. Now you can loop through the records and add items to an array. So you can control which items goes to which list. Some Pseudo code:
$updates = array();
$last_hr=0;
$last_item='';
while ($row=mysql_fetch_assoc($r
if ($row['update_time'] - $last_hr > $required_time_limit || $row['update_type'] != $last_item){
$last_hr = $row['update_time'];
$last_item = $row['update_type'];
}
$updates[$last_item][$last
}
Don't expect this code to work off-the-shelf you have to improvise on this.