not sure if this is possible to do but...
i have a follow function on my cms and would like a form of 'trending' posts that would allow me to show the posts that have gained the most followers over the past 7 days or so.
If i have a way of counting the amount of followers for today, is there a way i can check it against a past amount?
Or whats the best way around this? should i possibly be creating a script that will run a query on all the blogs, count there followers and store that in a DB field? I would make this happen every 7 days and run a check against this number and then the current follower amount?
Is this possibly over complicating things?
Thanks, Craig.
It looks like you are headed towards a Cron Job that will run periodically
If i have a way of counting the amount of followers for today, is there a way i can check it against a past amount?
Yes, save your data in a table just for this purpose. Then setup a script that checks this special table in your database where the stats are kept, and chooses the top ones. This table get's updated everyday, say at 00:00. With the data saved, you may query it to find out as much as you're saving in it
TLDR: this is kind of a project in itself, but I'm no expert
Related
I'm doing a school project in which I have to put news in an SQL database and, when a certain time passes (around a week or so) automatically edit the database to notify that a specific piece of news is no more active.
For example, I put on a news on monday, and I want it to automatically disappear (or appear as "closed") on the morning after.
So, what I would need is a PHP function (or an SQL one) that automatically starts with a specific timeout and edit the database.
Does anyone know something like that?
Thank you in advance for your help
PS I'm using PHP 5.3.10 and SQL 3.4.10 and I'm stick to them because they're installed on my lab computers
I don't think so. You would use a cron script or something similar to do this on a timer - but this is the wrong approach anyway.
What you need to do is add a time constraint to your SQL. So that your SQL "gets all the news articles for the last 7 days" or whatever
You can do this as follows:
SELECT * FROM news WHERE date > DATE_SUB(NOW(), INTERVAL 1 WEEK)
That way every time a user goes to your page, they will get all the records within a week of the current time. And you don't have to delete anything.
Remember the PHP and SQL query are only run when the website is refreshed. Hope that makes sense.
I'm trying to learn how to code a website algorithm like Reddit.com where there are thousands of posts that need to be ranked. Their ranking algorithm works like this (you don't have to read it, its more of a general question that I have): http://amix.dk/blog/post/19588
Right now I have posts stored in a database, I record their dates and they each have an upvotes and downvotes field so I'm storing their records. I want to figure out how do you store their rankings? When specific posts have ranking values, but they change with time, how could you store their rankings?
If they aren't stored, do you rank every post every time a user loads the page?
When would you store the posts? Do you run a cron job to automatically give every post a new value every x minutes? Do you store their value? Which is temporary. Maybe, until that post reaches its minimum score and is forgotten?
I would definitely not calculate their rank every time you display them.
A simple, and not so performant solution would be to cache post rankings, and once one post's ranking changes, you clear or refresh the cache.
That is not ideal, but it is possible.
Another way would be to do as you alluded to: calculate and store ranks in the database (and ideally cache them), and then refresh those rankings using a cron job every x minutes.
Again, these are basic approaches to what you want to do. You can then build on them over time.
The algorithm you choose will most likely be very particular to your needs.
You need to also gauge what kind of traffic your site would be getting, as it would dictate what kind of lengths you should go through to get the right algorithm.
I would instantly calculate a score for the single vote on a time-weighted scale. I would send that score into a queue or use it to increment a field depending whichever of those is performant for you.
At a regular time interval, I would take all currently ranked articles and all articles that have received votes during the time window and rescore all ranked articles followed by all the queued articles in descending order of score until I had calculated enough to fill my ranking quota.
The ranking list would be cached and used until the next ranking cycle. You'll have to adjust the queue retention period (maybe anything that had activity in the last N queues is re-queued), retention of articles, etc. based on your site load, but this should be a well-performing starting point.
If you're using the exact algorithm reddit uses, you only need to change the ranking field whenever an item is voted up or down - and really only when the difference between upvotes and downvotes changes orders of magnitude. This article explains a little more about how their ranking works.
http://bibwild.wordpress.com/2012/05/08/reddit-story-ranking-algorithm/
Basically, the up and down votes only serve to "displace" the posts.
If D is the difference between the number of upvotes and downvotes, a post is shifted up or down 12 hours per order of magnitude of D. Other than that, it's just a simple time ranking.
if however you want to use your own ranking system where age of the post matters in some way other than linearly, you'll have to either create an indexed field and recalculate the rankings at time intervals as has been said, or just put your sorting into your SQL query, as I've said in my comment. But chances are, you can find a way where it doesn't have to be recalculated over and over.
Ok I know the title doesn't really tell you what my problem is but I'll try it now.
I am developing a game. People can subscribe their animals for a race. That race starts at a specific time. It is a race for which ALL users can subscribe. So the calculation of which animal is first, second etc. happens in an php file that is executed, every 2mins there is a new calculation for about 1h. So there are 30 calculations. But ofc. this code is not connected to the logged in user. The logged in user can click on the LIVE button to see the current result.
Example: There is a race at 17.00 later today. 15 animals subscribed, from 4 players and they can all check how their animals are doing.
I do not want someone to post me the full code but I want to know how I should let a php code run for about 1 hour (so execute code, sleep 2min, new calculation, sleep 2min and so on) on my server or so. So it is not connected to the user.
I thought about cron jobs but that is really not the solution for this I believe.
Thank you for reading :p
Two approaches:
You use an algorithm which will always come to the same conclusion, regardless of when it is run and who runs it. You just define the starting parameters, then at any time you can calculate the result (or the intermediate result at any point in time between start and finish) when needed. So any user can at any time visit your site and the algorithm will calculate the current standings on the fly from some fixed starting condition.
Alternatively, you keep all data in a central data store and actually update the data in certain intervals; any user can request the current standings at any time and the latest data from the datastore will be used. You will still need an algorithm that has traits of the one described above, since you're likely explicitly not actually running the simulation in real time. Just every x seconds, you run your calculations again, calculating what is supposed to have changed from the last time you ran them.
In essence, any algorithm you use needs this approach. Even a "realtime" program simply keeps looping, changing values little by little from their previous state. The interval between theses changes can be arbitrarily stretched out, to the point where you calculate nothing until it becomes necessary. In the meantime, you just store all the data you need in a database.
Cron jobs are the wright way i think. Check this out when you are not so good with algorithm:How To: PHP with Cron job Maybe you have to use different cron jobs.
i am going to have achievements such as
get 1000 points
login 20 times
complete 2 tasks within 24 hrs
etc
what is the best way to program this? (like, the logic behind it. it would be a wast eof reseouces to check every possible achievement on every page load)
This is a fairly open-ended question. One approach is to store progress for what users have already achieved in a database table (one column for each achievement) and every time progress is made, up the progress value. A magic value for each achievement (null, 1 million, whatever) can be set when the target is reached and then you just need to check which columns in each user's entry have the magic value.
You could just store the progress and calculate the achievement status on each access, but I think that would be problematic for things like the time-limited tasks you mention and people might get irked if you change the goals for any reason and they lose achievements they had previously gained.
An arguably more correct approach would be to have a static "Achievements" table in which each row details one achievement, along with the goals. You'd then have a separate "Progress" table in which each user would have a row for every different achievement they are working towards. This is more work to implement, but will allow you to add / remove / alter goals much more easily at a later date than hard-coding them to column names in your progress table. I don't think you should underestimate the value this will give you later on. This way, the progress table can have multiple columns for each user / achievement combination, which allows you to store things like time achievement reached etc. and you can very simply get a list of achievements like this:
SELECT achievement_id
FROM progress
WHERE user_id = $user
AND achievment_time IS NOT NULL
You've got to check something for each page load if you want to display achievements on each page, but if you record progress in one of these ways, you're not going back to first principles and calculating how many logins have been made, how many tasks have been completed, etc. every time - just a SELECT from your DB, which I assume is what you mean.
Recording progress is simple for things like number of logins (just increment a counter each time they perform the action), but you'll obviously have to think a bit more carefully about the logic for keeping tabs on progress for time-limited achievements. Here, you'll need to keep track of timestamps for when actions were performed in a separate table (you may already be doing this, of course) and each time a new timestamp is created, count those in the preceding time period to see if the achievement has been reached.
Ok I wanted to get the communities thoughts on the use of Views in MYSQL vs. in page calculations using php.
I am about to start trying to design a page which relies on retrieving data from my database and working out averages and totals based on the data it retrieves. My initial thoughts were that I should retrieve it in the normal way as if I were going to display each record and then calculate the averages and totals in php before displaying. However before I go to the trouble and no doubt hours of tinkering that it will take for an amateur like me to get it working, I wanted to know if it may well be worth setting up a view instead and just retrieve the information from that with a simple select command?
The data that it will be working the avergaes and totals out for is updated weekly and in an ideal world I would want my averages and total to be year to date until the end of the first year, at which point a total will continue running but a new year to date would commence.
Any ideas or suggestions would be great.
Alan.
I would think it is faster to let PHP do the calculations. I use PHP for this on my sites, though I've not done any speed tests myself...