Page 1 of 1

Random Weekly Quote Code Question

Posted: Tue Oct 12, 2004 11:20 am
by lunarstudio
Hi. I am new to this forum. My knowledge of PHP is VERY limited but I somehow manage to get by most of the time (usually by grabbing snippets of other people's code which makes me feel rather bad and inept.) I'd like to really get to know PHP better but the majority of my time is unfortunately preoccupied with graphics instead.

I have a PHP script that pulls random quotes from a text file into a webpage. This works fine. Only problem is that the person that wants this feature incorporated wants these quotes to be selected on a weekly basis instead of every time one refreshes the page.

Code: Select all

<?php
		$RANDOM_TXT_FILE = "quotes.txt";
		srand((double)microtime()*1000000);
		if (file_exists($RANDOM_TXT_FILE)) {
		$arry_txt = preg_split("/--NEXT--/", join('', file($RANDOM_TXT_FILE)));
		echo $arry_txt[rand(0, sizeof($arry_txt) -1)];
		} else {
		echo "Error: can't open $RANDOM_IMG_FILE file";
		}
?>
I guess for that matter, these quotes do not even have to be random but can be sequential as long as it loops at the end. But this would be calling up an entirely different script which I would have no idea a how to code. If someone could help walk me through on these two items (or at least post some code for it), then you would have my utmost gratitude and appreciation.

Thanks,

Chuck

Posted: Tue Oct 12, 2004 2:40 pm
by Weirdan
hint: [php_man]date[/php_man]('w') would give you the number of the current week
hint 2: you can use the modulos (%) operator to get remaider of division:

Code: Select all

echo 7%2; // yields '1'
echo 7%5; // yields '2'

Posted: Tue Oct 12, 2004 2:47 pm
by lunarstudio
Wow - I appreciate your response though I have know idea how to implement that. I'd imagine there would need to be some sort of cookie for weekly randomization and that this alone wouldn't take care of this.

Posted: Wed Oct 13, 2004 9:44 am
by pickle

Code: Select all

$week_num = date('w');//get the current week
$quote_num = 52%$week_num;//modulus it so the quote always loops
$file_contents = file('quotes.txt');//retrieve file contents.  
                                    //file() puts each line of the file into a different array element
$random_quote = $file_contents[$quote_num - 1];//retrieve the appropriate line
Keep in mind, this won't be completely random, but rather, sequential.

PHP Manual:
[php_man]file[/php_man]()
[php_man]date[/php_man]()

Posted: Thu Oct 14, 2004 1:39 pm
by lunarstudio
Thanks Pickle! I really appreciate your coding this. I just don't understand how PHP recognizes some of these terms- lack of familiarity I guess. Is it mainly getting all the info from a hard-coded 'date('w') paramter'? I haven't had a chance to, but I am looking forward to implementing this script within the next few days.

Posted: Thu Oct 14, 2004 1:46 pm
by Joe
You should consider using a database table to do this type of work.

Posted: Thu Oct 14, 2004 2:49 pm
by pickle
date() by default takes a string as the first argument. That string can contain reserved characters that determine what date() will return. If you don't pass it a second argument (a UNIX timestamp), date() will just use the value returned by time() (which it calls behind the scenes). It's all explained in the documentation (linked to in my last post). 'w' will return the week of the year. So, if you pass date() 'w' as the first parameter, and the timestamp for December 30 as the second argument, you'll get '52' returned. Again, that's all explained in the documentation.

And yes, putting the quotes in a db would make things a bit easier, much cleaner and arguably, faster.