I have questions stored in my database. I want to regularly post one question on my website from the database at a 24 hr interval automatically. Is there a way I can do that ?
9 Answers
You can do this with steps:
- Create normal PHP-script which will post your questions.
- Schedule your script with standard OS scheduler. It is cron for *nix (Win-versions exist too) or AT for Windows. To define certain interval - you should read scheduler's manual (for
cronformat is provided here)
Example (cron)
0 2 * * * /usr/bin/php /path/to/insert/script.php
-in this case every day at 02:00 AM cron will try to execute command /usr/bin/php /path/to/insert/script.php - i.e. if your script.php will extract your question from DB and post it - that will do the stuff.
3 Comments
CRON job will actually be doing - The question is already in the database!Yes you can do it by using Cron job . Set time interval and your file script location. It will automatically hit your script on that time interval.
Here is good tutorial : http://docs.phplist.com/SetupCronJob.html
Comments
Providing you could create a PHP script to select a different question each time, all you'd need to do would be to set up a cron to run the PHP script every 24 hours. You can find more info on cron here.
Comments
You should look into MySQL date functions.
A contrived example would be using CURDATE():
SELECT * FROM questions WHERE publish_date = CURDATE()
Storing the publish_date will mean you can dynamically load the question when that date arrives.
Comments
Yes, you can. I will shortly outline the two most common solutions. The difficulty rises that PHP is not an always running program, but is a language executed on request and then shutdown on completion.
- Have some sort of init.php file on your webserver which is being included on every page. That script will check whether the time has passed since last question, and push a new question.
- On the other hand, you can add a cronjob which will execute your php script pushing the question. This solution is more robust, but requires access to a webserver you might not have.
Comments
Create a php file put the code to fetch question form your database then set cronjob to excecute the file on perticular time or also you can execute file by including it your login or any other page which lods first by including that php file file so that when first user logs in it will execute.
Comments
Steps
Create a PHP script to select and post a particular question randomly.
In your main php script write an AJAX method(which will load the PHP script) which can be called using
setInterval()using the following syntax-setInterval("AJAX_fun()", 24*3600*1000);
This statement will call the AJAX function in a periodic interval of 24hrs. For that you must know AJAX. I mean what should be the body of the AJAX to load the PHP script that you must have an idea of.
Another alternative
You can simply reload the page using javascript setInterval() function
i.e. <script>setInterval("window.location.reload()", 24*3600*1000);</script> and before that you have to select a question from the database randomly using a PHP logic.
AJAXcan't help here.