Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 14th, 2005, 11:21 PM   #1
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
PHP random quote generator script

Well technically this little php snippet doesn't generate random quotes, that would just be weird However, it does select a quote randomly from a user generated quote database.

<?php
	function quote_generate()
	{
		$quote = array("An apple a day keeps the doctor away.",
		"A bird in the hand is worth two in the bush.");

		return($quote[rand(0, count($quote) - 1)]);
	}

	echo quote_generate();
?>

Simple and effective methinks. To add your own quotes just enclose the string in " " and separate each of them by commas. I use a similar variant of the script on my site to have random quotes pop up in the news page when people visit the site.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Sep 15th, 2005, 1:06 AM   #2
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Good, this could be quite cool on a website, i think i have seen something like it before.
If you are working on a project with documentation, whenever someone loads the homepage it could select a "Tip of the Day".
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Sep 15th, 2005, 9:12 AM   #3
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
heh, it's just like fortune in linux!
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Sep 15th, 2005, 12:31 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
If you store the quotes in a file, separated by line breaks, you can load them like this:
[php]$quotes = file('quotes.txt');[/php]
Easy, no?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Sep 23rd, 2005, 7:00 PM   #5
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
An even quicker way of sorting random quotes from a file could be...

[php]
printf("The quote of the day is: <b>%s</b>\n", array_pop(shuffle(file("quotes.txt"))));
[/php]
__________________

tempest is offline   Reply With Quote
Old Sep 24th, 2005, 12:36 AM   #6
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 403
Rep Power: 3 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Isn't it faster (memory access wise) to have the quotes inside the function as I had it in the original post? Rather than storing them in a separate file? I fail to see the benefits. Perhaps something to do with editing them?
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Sep 24th, 2005, 1:30 AM   #7
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4 linuxpimp20 is on a distinguished road
couldn't you create a database with MySQL with the quotes and hav eone column be a unique id numer and then just use PHP to generate a random number and then query the quote that unique id numer matches the quote? Hell i imagine it wouldn't much too much more trouble to have another php script that allowed the user to make the quotes to populate the database with so it would randomly generate user defined quotes instead of having to insert the quotes into the code.
linuxpimp20 is offline   Reply With Quote
Old Sep 24th, 2005, 1:50 AM   #8
raspberryh
Newbie
 
raspberryh's Avatar
 
Join Date: Sep 2005
Location: Orlando, FL
Posts: 2
Rep Power: 0 raspberryh is on a distinguished road
linuxpimp, that sounds cool, but what if the user wants to delete a quote? then say they delete quote with id 3, and then the code randomly generates a 3, then when you do your "select * from quotes where id=3" you will get no result.
but i think instead, you could generate a random # between 0 and the total # of quotes. then do "select * from quotes limit [random#], 1" so it picks the [random#]-th one.
raspberryh is offline   Reply With Quote
Old Sep 24th, 2005, 2:35 AM   #9
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4 linuxpimp20 is on a distinguished road
that is true. I did not think of that. Im just getting into php programming so only read a little about php and mysql.
linuxpimp20 is offline   Reply With Quote
Old Sep 24th, 2005, 2:40 AM   #10
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
It's good to try and find out where errors could possibly occur, and try to make your program bugless.
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:44 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC