Random Weekly Quote Code Question

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
lunarstudio
Forum Newbie
Posts: 3
Joined: Tue Oct 12, 2004 10:52 am
Location: Boston

Random Weekly Quote Code Question

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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'
lunarstudio
Forum Newbie
Posts: 3
Joined: Tue Oct 12, 2004 10:52 am
Location: Boston

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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]()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
lunarstudio
Forum Newbie
Posts: 3
Joined: Tue Oct 12, 2004 10:52 am
Location: Boston

Post 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.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

You should consider using a database table to do this type of work.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply