Posting random text for 7 days??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kline
Forum Newbie
Posts: 3
Joined: Mon Jul 21, 2008 8:22 pm

Posting random text for 7 days??

Post 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.
gmapsuser
Forum Newbie
Posts: 5
Joined: Mon Jul 21, 2008 11:47 pm

Re: Posting random text for 7 days??

Post 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,
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Posting random text for 7 days??

Post 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.
kline
Forum Newbie
Posts: 3
Joined: Mon Jul 21, 2008 8:22 pm

Re: Posting random text for 7 days??

Post 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. ....
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Posting random text for 7 days??

Post 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.
manixrock
Forum Commoner
Posts: 45
Joined: Sun Jul 20, 2008 6:38 pm

Re: Posting random text for 7 days??

Post 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).
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Posting random text for 7 days??

Post by Stryks »

Hey .... not bad.
kline
Forum Newbie
Posts: 3
Joined: Mon Jul 21, 2008 8:22 pm

Re: Posting random text for 7 days??

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Posting random text for 7 days??

Post 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!)
Post Reply