![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
King of Portal
|
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 |
|
|
|
|
|
#2 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() |
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! |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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? ![]() |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
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]
__________________
|
|
|
|
|
|
#6 |
|
King of Portal
|
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 |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Sep 2005
Location: Orlando, FL
Posts: 2
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
that is true. I did not think of that.
Im just getting into php programming so only read a little about php and mysql. |
|
|
|
|
|
#10 |
|
Expert Programmer
|
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|