Page 1 of 1

Posting random text for 7 days??

Posted: Mon Jul 21, 2008 9:42 pm
by kline
I've been using a randomizer to post various things for several years. Now that I am redesigning certain pages, I want to print/post a whole bunch of text. But I want the text (randtext/text.inc) file from Sunday to Sunday.

Because my jokes, quips, and so on change with every refresh, the code is trivial::

Code: Select all

 
if ($random){
  $line = rand(0,$number-1);
}
 
then below,

Code: Select all

 
 
<?
 
// display the text on the page
 
echo "$text[$line]";
 
?>
 
 
My questionis, what kind of php date code would keep the ``echo''
line the same for a full week? and refresh or change at, say 0.00 hours every Sunday?

thanks for any insights!

gary kline

PS: "$random" is initialized to true.

Re: Posting random text for 7 days??

Posted: Mon Jul 21, 2008 11:52 pm
by gmapsuser
You can deal this issue by using cron job if your application is running on a linux box or a task scheduler if it is a windows OS. You can set the file to run once every week. Hope this will solve your issue.


Cheers,

Re: Posting random text for 7 days??

Posted: Tue Jul 22, 2008 12:44 am
by Stryks
Alternately, save an expiry timestamp to a database or file and check that value on every page view. If expired, then select a new random phrase, and update it, along with a new expiry timestamp.

Re: Posting random text for 7 days??

Posted: Tue Jul 22, 2008 1:08 pm
by kline
Yeah, cron was the first thing I thought about, but would that actually work?? And I thought about using a mysql entry, but oinly as a *last* resort!

What I thought of first was to test if the date was Sunday at midnight; if so, then run thru the random routine. I don't know enough about PHP yet to code the `date` or date() stuff.

Thoughts?

gary

PS: I thought I had this selected to auto-mail me when there was a response. ....

Re: Posting random text for 7 days??

Posted: Tue Jul 22, 2008 8:02 pm
by Stryks
The only way I could see the midnight check would be to store the last time it was checked somewhere, be that mysql or elsewhere. The reason being that it would only work if someone viewed the script at exactly the right time to enact the change.

You could catch the closest view by checking to see if it was after the preset time, but of course, without setting the last change time, every second of every day is after any given weekly time.

Cron would certainly do the job for you though, Just create a script to make the change and set it to run at a regular scheduled time.

Re: Posting random text for 7 days??

Posted: Tue Jul 22, 2008 8:32 pm
by manixrock
Use php's date() fuction:

Code: Select all

$jokesByWeek = array(
    'Patient: Doctor! Doctor! Everyone keeps on copying me! Doctor: Doctor! Doctor! Everyone keeps on copying me!',
    'Light travels faster than sound. This is why some people appear bright until you hear them speak',
    'What\'s the new and politically correct name for Lesbian? Vagitarian.',
    ...
);
$weekOfYear = intval(date('W'));
$jokeOfThisWeek = $jokesByWeek[$weekOfYear % count($jokesByWeek)];
 
The last modulus operation ("%") is if you don't have enough jokes in the array (there are 41 weeks in a year).

Re: Posting random text for 7 days??

Posted: Tue Jul 22, 2008 8:40 pm
by Stryks
Hey .... not bad.

Re: Posting random text for 7 days??

Posted: Sat Jul 26, 2008 7:57 pm
by kline
I see that my last post commenting on the above code didn't make it before this domain went down for an upgrade.

Long-story-short, I came up with a 80+line script that ought to handle all cases. The randomized text will appear on my "www" page and _will_ have a link to my virtual site URL. I forgot to include the <A HREF....></A> markup and have to re-cat the files.

What got-me was thinking that this code would take a few hours--including testing. At least now (soon), the script will make things much easier on my readers... .

thanks guys.

Re: Posting random text for 7 days??

Posted: Sat Jul 26, 2008 10:28 pm
by califdon
manixrock wrote:The last modulus operation ("%") is if you don't have enough jokes in the array (there are 41 weeks in a year).
Whaddaya do the other 11 weeks?? :rofl:

(I do realize that your fingers were off by one key on the keyboard, but I couldn't resist!)