2

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
  • 1
    if you are on some server you can initialize it by setting a crone job. simply write a script for fetching data from database and attach it with the crone job. Commented Sep 26, 2013 at 11:27
  • Somehow AJAX can help. Commented Sep 26, 2013 at 11:30
  • @RajeshPaul AJAX can't help here. Commented Sep 26, 2013 at 11:32
  • Please explain to me what you mean by "post one question" - Surely all that is required is to display a question depending on the current or specific date? Commented Sep 26, 2013 at 11:35
  • @AlexP there is no date dependency in his problem. Is there??? Commented Sep 26, 2013 at 11:49

9 Answers 9

1

You can do this with steps:

  1. Create normal PHP-script which will post your questions.
  2. 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 cron format 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.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm still confused by what this CRON job will actually be doing - The question is already in the database!
@AlexP - I've added the sample
Thanks - I agree it's a valid answer, I just think calling a CRON job is unnecessary in this case as it would mean a update of a stored flag or filter on a date column etc - If this is the case you could just do it dynamically where you display the questions - See my answer for better idea what I mean.
0

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

0

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

0

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

0

The only way to do it correctly is to use cron jobs. You should take a look at the administration panel of your hosting service.

Comments

0

Write a script that will post one question on your website everyday and set a cron job to run that script once a day and you are done.

How to set a cron job , ask you hosting service provider , most of the hosts have this feature in cpanel

Comments

0

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.

  1. 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.
  2. 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

0

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

0

Steps

  1. Create a PHP script to select and post a particular question randomly.

  2. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.