henricar Posted March 27, 2011 Share Posted March 27, 2011 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 !!! Quote Link to comment https://forums.phpfreaks.com/topic/231884-storing-dynamic-values-in-mysql/ Share on other sites More sharing options...
gristoi Posted March 27, 2011 Share Posted March 27, 2011 If your using mysql > version 5 you can write a mysql trigger to listen to inputs on the other tables and adjust the value accordingly. Have a read of ; http://dev.mysql.com/doc/refman/5.0/en/triggers.html Quote Link to comment https://forums.phpfreaks.com/topic/231884-storing-dynamic-values-in-mysql/#findComment-1192987 Share on other sites More sharing options...
henricar Posted March 28, 2011 Author Share Posted March 28, 2011 hey thanks for the reply! but this method seems quite advanced and I'm kind of a php noob.. would you be able to suggest another way I could attain this goal? thanks Quote Link to comment https://forums.phpfreaks.com/topic/231884-storing-dynamic-values-in-mysql/#findComment-1193426 Share on other sites More sharing options...
jcbones Posted March 28, 2011 Share Posted March 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231884-storing-dynamic-values-in-mysql/#findComment-1193455 Share on other sites More sharing options...
henricar Posted March 28, 2011 Author Share Posted March 28, 2011 great, thanks! will give the triggers a thourough read, and you're right: learning new skill = good Quote Link to comment https://forums.phpfreaks.com/topic/231884-storing-dynamic-values-in-mysql/#findComment-1193488 Share on other sites More sharing options...
henricar Posted March 29, 2011 Author Share Posted March 29, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/231884-storing-dynamic-values-in-mysql/#findComment-1193492 Share on other sites More sharing options...
jcbones Posted March 29, 2011 Share Posted March 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231884-storing-dynamic-values-in-mysql/#findComment-1193922 Share on other sites More sharing options...
henricar Posted March 30, 2011 Author Share Posted March 30, 2011 hey here is the exact stucture of the 3 tables concerned. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/231884-storing-dynamic-values-in-mysql/#findComment-1194327 Share on other sites More sharing options...
henricar Posted March 31, 2011 Author Share Posted March 31, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/231884-storing-dynamic-values-in-mysql/#findComment-1195178 Share on other sites More sharing options...
jcbones Posted April 2, 2011 Share Posted April 2, 2011 You could set a crontab (*nix server), or event through the task manager (IIS server). This will update based on criteria you set. Quote Link to comment https://forums.phpfreaks.com/topic/231884-storing-dynamic-values-in-mysql/#findComment-1195774 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.