Jump to content

storing "dynamic" values in MySQL?


henricar

Recommended Posts

Hey guys, I'm new here so hello... but I'll get right to the point:

 

I have a table named POSTS, where I store user posts. In this table, I have a row named "Popularity". Now I would like the value of "Popularity" to be a function of the number of Likes, Dislikes, Views and Date posted, all of which are stored in respective tables (linked by Primary and Secondary keys).

 

I was wondering if it actually is possible to have the value of "Popularity" adjust automatically when a change in "Likes", "Dislikes" or "Views" occurs. And if not, what alternatives I have.

 

The ultimate reason I need to have this value stored in a table and up to date is that I wish to sort posts by "Popularity".

 

Many thanks !!!

Link to comment
Share on other sites

There is no better way, and also nothing like learning a new skill.  Otherwise this will take multiple queries, and cause your pages to slow down.

 

In the off hand that you want to go with a slower route, you could write separate queries for the likes, dislikes, views, and date_posted tables to run after you insert query finishes.

 

If you wish to create triggers, it is a simple as running this in phpMyAdmin, or similar software.

CREATE TRIGGER likes AFTER INSERT ON likes FOR EACH ROW UPDATE `POSTS` SET Popularity = Popularity + 1;
CREATE TRIGGER dislikes AFTER INSERT ON dislikes FOR EACH ROW UPDATE `POSTS` SET Popularity = Popularity - 1;

 

ETC, until you have everything calculating how you want.  This way, all you have to worry about in the script, is inserting the data to the respective tables.

 

 

Link to comment
Share on other sites

seem to have run into a little problem though :/... the algorithm for popularity is P = (20*likes -20*dislikes + views)/(today's date - date posted). Is it possible to make this work? like have a trigger occur daily to calculate the difference between today's date and the date posted?

Link to comment
Share on other sites

So, now your trigger will look something like:

DELIMITER ;;
CREATE TRIGGER dislikes 
AFTER INSERT ON Dislikes
FOR EACH ROW
BEGIN
DECLARE disliked INTEGER;
DECLARE liked INTEGER;
DECLARE date INTEGER;

SELECT COUNT(*) INTO disliked FROM dislike WHERE linked_key = 'NEW.linked_key';
SELECT COUNT(*) INTO liked FROM likes WHERE linked_key = 'NEW.linked_key';
SELECT DATEDIFF(NOW(),date_posted) INTO date FROM date_posted WHERE linked_key = 'NEW.linked_key';

UPDATE POSTS SET Popularity = ((20*liked)-(20*disliked))/date WHERE linked_key = 'NEW.linked_key';

END;;
DELIMITER;

 

I could help you more, but I would need some table structures.  I have no clue what columns link the tables together.

Link to comment
Share on other sites

like I understand the code you previously posted but it still we be updating "popularity" only when a post get liked, viewed or disliked. I still need to popularity to fluctuate on a dailay basis given the spread between current date and date posted is growing by the day. any solution to this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.