Forum Moderators: coopster

Message Too Old, No Replies

How do I refresh an action

none

         

twistydog

5:31 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



I have this php code that will automatically grab a random line from this text document. The problem is that everytime you refresh the page it will grab a new random line. Is there anyway I can set it up so that it only randomly grabs a new line once per day?

Thank you very much in advance.

-Brad

jatar_k

5:35 pm on Jul 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld twistydog,

maybe have a little script that runs once a day (maybe via cron) and then stores the random line in a text file or something and your page can just include that text file.

twistydog

8:58 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



Hey, thanks for tip. But isn't there something easier that I can just throw into the HTML. Right now my code looks like this:

<?

$random_content="../random_content.txt";
$random_content=file("$random_content");
$display=rand(0, sizeof($random_content)-1);
echo $random_content[$display];

?>

So is there anyway to just have that code only run once per day?

I know crons sounds like an easy solution, but my host would not let me run as many crons as I would need.

jatar_k

9:00 pm on Jul 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well you need something to compare to

hence,

run the script once per day via cron
or
use a predetermined time, so you would probably have to store the time it last ran

twistydog

9:30 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



Now that I think about it the cron probably won't work anyways. I am putting this code on many pages. So I guess every page would have the same random content. I would need them to all have something different from the random_content.txt

I think all of this is going to require a lot more work than its really worth. I thought it would be easier.

jd01

9:52 pm on Jul 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could do this with a fairly small DB...

1. Set up a DB with id, pagename, rotating_text, date

2. Use the php $_SERVER to get the page name that is being accessed.

3. Set the sql SELECT to the page name as the variable.

4. Compare the sql['date'] to getdate() if the date is more than 24*60*60 greater than sql run the script, store the information and display the page. Else, show the information.

It might take a little longer for your page to open when the information needs to be changed, but that would only be once on the first open after the information in the DB has 'expired'. The comparrison should not take too much time.

Justin

Edited: removed #5 redundant.