简体   繁体   中英

Group similar activities in a activity feed

After searching Google and Stackoverflow for similar questions, i still haven't found any solution.

One of the main parts of my site are activity feeds. It show all the actions different users did just like Facebook. I store all the activities in a MySQL table, and render the view with PHP.

I want to group similar activities just like Facebook does:

*Current situation:*
 - User A uploaded a picture
 - User A uploaded a picture

*Desired situation:*
 - User A uploaded 2 pictures

My activity table looks like this:

[Activities]
  - i_id
  - a_source
  - a_type
  - a_parent
  - a_parent_id
  - a_data (json string with meta data, photo filenames for example)

I have found these questions on stackoverflow, but none of them provide a solution:

  1. https://stackoverflow.com/questions/5844655/how-to-group-similar-items-in-activity-feed-similar-to-facebook
  2. User activity feed (ala facebook). How to group similar activities?
  3. How to group similar items in an activity feed

:edit:

I'm looking for an approach and want your thoughts on how to solve this.

Would it be best to run a cron job and group all the same items in the db? Or would it be better to do this on render time with php?

I prefer a (realtime) php solution but then again i don't know where to start.

I just need a starting point for future reading.

I'm looking for an approach and want your thoughts on how to solve this.

Short answer? GROUP BY

Answer based purely on assumptions? Below:

Coding blindly, since you haven't provided anything else aside from the situations and table columns:

Assumed data for columns that appear to make sense at the moment (again, without seeing any data):

i_id    a_type
123     photo upload
123     photo upload
123     comment

With: i_id -being a user id a_type -being a type of activity

Just do:

SELECT a.i_id, a.a_type, COUNT(1) AS activity_count
FROM activities AS a
WHERE a.i_id = 123 -- or convert this to a parameter through PHP->PDO
GROUP BY a.i_id, a_type

The SQL above should give you:

i_id    a_type          activity_count
123     photo upload    2

Would it be best to run a cron job and group all the same items in the db? Or would it be better to do this on render time with php?

Do you need aggregated data? Does the activities table have millions and millions and millions and.... of data or does it expect to have that much data? Do you need reports on summaries of activities per user? Will you mostly utilize the table for reports? Will you be partitioning this table and archive data out hence needing only aggregated data for future checks or reports?

I prefer a (realtime) php solution but then again i don't know where to start.

You can search in google for real PHP solutions. This stackoverflow isn't likely to be the place where you'll get one. Links, maybe.

I just need a starting point for future reading.

LINK: On GROUP BY

LINK: On PDO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM