So, I have a forum and I want to reward users who donated automatically with a special membergoup, I can do this by adding it into forum's MySQL database. What I need now is to remove such usergroup when a month has passed, automatically. What I would like to know is if it would be possible to make it purely on PHP for example, or another similar alternative.
Thanks in advance
php has no job scheduling mechanism built into it, unless you run a script as a daemon. MySQL has its own scheduling mechanism: http://dev.mysql.com/doc/refman/5.1/en/events-overview.html
You could store the date that the donation was made, and then, you could check the difference between that date and the current date.
If the result is > month then the user should not be in that membergroup.
Also, to avoid calculating that difference every single time, you could have an extra field in your database for each user, which would indicate if that user should be considered a donator.
Related
My host (blueangelhost.com) claims that I can't use the event scheduler because it takes up too many resources. I have access to cron jobs in cPanel, but I've tried and they don't seem to work.
So, my question: Is there any kind of efficient PHP code that will automatically truncate a MySQL table in a database?
Well, if it needs to be automatic, or at a specific time, not really. But you could have your website trigger the script when someone gets on it, here's the approach you could use:
On a script that is run on every page (header, menu,footer, layout):
Check in DB or file, the date of the last truncate;
If the date is yesterday, run the truncate;
Change DB or file and put current date;
This way, it will execute once a day. But never at the same time, and not if no one walks on your website for a whole day.
Lets say there is a thread ( on forums ) which will be active for 3 days only. Now, after 3 days, I want this thread automatically closed.
Can I use reference to time when this thread is created in database, and than make if statement if current date + days is bigger than date created, I will print out "<h2>Thread Closed for posting</h2>"
And when I consider some other tasks, I suppose I can use reference to time and have certain event executed on this.
Am I right?
You can use a cron (ran every minute) to set a status field on the thread table to closed such as.
UPDATE threads
SET status='closed'
WHERE lastPost+INTERVAL 3 DAY<NOW()
Then in PHP something such as
if($thread['status'] == 'closed')
{
// Put your HTML here.
}
A 'cron' is a task that runs at a specific interval or time. This means that it should be used for tasks that must be done without user interaction. For example, a backup, automated emails or pulling data from a remote service.
What you want is better suited to a condition on checking whether a thread is closed. Rather than just having a flag, you also check the age. This means you can change your old-thread logic without needing to edit the database.
You could make a PHP script that gets executed by cron (read up on how to execute PHP in the command line) that SELECTs all the posts in a certain date and then sets them to closed. If you ran that, say, twice a day, then you could do a good job in getting all those posts closed.
Good reference on using cron to run PHP
I have a database with a table and some columns. One of these columns is flags (similar to SO) where users can flag comments. I would like to give each user 5 flags per day. So if a user uses 2 flags in a 24 hour period, the flags should reset to 5 at the end of the 24 hours. I really have no idea how to do this. Is there a special mysql function?
PHP:
$query=mysql_query("UPDATE users SET flags='5' WHERE userID='$user'");
how would i get this to repeat every 24 hours? (if this is the right solution)
The best way is probably to set up an automated task (possibly using cron) that runs a query to do this.
Make a script and add to cron job. It will automatically update all on specified time of day.
Cron is very simply a Linux module that allows you to run commands at predetermined times or intervals. In Windows, it’s called Scheduled Tasks. The name Cron is in fact derived from the same word from which we get the word chronology, which means order of time.
Using Cron, a developer can automate such tasks as mailing ezines that might be better sent during an off-hour, automatically updating stats, or the regeneration of static pages from dynamic sources. Systems administrators and Web hosts might want to generate quota reports on their clients, complete automatic credit card billing, or similar tasks. Cron has something for everyone!
You can use MySQL Event Scheduler and define an event to let it update every interval you want.
I'm writing a realtime wep application, something similar to auction site. The problem is that I need a daemon script, preferrably php, that runs in background and constantly launches queries to mysql db and basing on some of criterias (time and conditions from resultsets) updates other tables. Performance of the daemon is crucial. Sample use case: we have a deal that is going to expire in 2:37 minutes. Even if nobody is watching/bidding it we need to expire it exactly in 2:37 since the time it started.
Can anybody advise a programming technology/software that performs this kind of task the best?
Thanks in advance
UPDATED: need to perform a query when a deal expires, no matter if it has ever been accessed by a user or not.
Why do you need to fire queries at time intervals? Can't you just change how your frontend works?
For example, in the "Deals" page, just show only deals that haven't expired - simplified example:
SELECT * FROM Deal WHERE NOW() <= DateTimeToExpire
Accordingly for the "Orders" page, a deal can become a placed order only if time hasn't expired yet.
Does your daemon need to trigger actions instantaneously? If you need a table containing the expired state as a column you could just compute the expire value on the fly or define a view? You could then use a daemon/cron job querying the view every 10 minutes or so if you have to send out emails or do some cleanup work etc.
For example I made a reservation for a restaurant and it expires in 24 hours. In the reservation table of the db (MySQL), how do I automatically update the status to expired after 24 hours? What approach would you guys suggest? Thanks in advance!
Don't really know the problem you're trying to handle but I'd store the timestamp when the reservation was made and have a field which stores after what interval from the point reservation was made does it expire (24 hrs in your case) and that's it. The rest should be handled at the point where you read/display that information.
Besides if you still really want to CHANGE the value in DB go for a cron that regularly updates the DB
Running periodical tasks is usually done with cron.
Here are some instructions how to use it on drupal (which is php/mysql)
Use cron like Unreason said or MySQL events, see Create event.
using cron can be liitle difficult
just run update query by comparing timestamp value with your time limit.
it will work.
I would suggest the following two methods.
To run a cookie at the background and check it out if the time (here in this case 24hrs) ran out or not..
Store the entry in the database with DATETIME type. As we check whether the user logged in or not with session variables. In the similar way for every click in the site, call a function to check whether the time is lapsed or not.