Log in

View Full Version : Script to show random quotes



Ryan Fitton
12-28-2007, 06:56 PM
Hi, i am looking for a script that can display random quotes that i have added. I want the script to display a diferent quote every time the page is refreshed. Help me please.:)

Twey
12-28-2007, 07:02 PM
In what language?

Ryan Fitton
12-28-2007, 09:51 PM
i have seen some written in PHP and some in Javascript but i was thinking if there was someway to use PHP and have it linking to an external file where the qoutes are kept.

Twey
12-28-2007, 11:06 PM
Yep, that's fine:
<?php
function random_choice($arr) {
return $arr[rand(0, count($arr) - 1) % count($arr)];
}

print random_choice(file('quotes.txt'));
?>

Ryan Fitton
12-29-2007, 10:38 AM
Thanks a lot :)

Ryan Fitton
12-29-2007, 10:43 AM
Yep, that's fine:
<?php
function random_choice($arr) {
return $arr[rand(0, count($arr) - 1) % count($arr)];
}

print random_choice(file('quotes.txt'));
?>

Sorry i have a little problem, how would i set out the quotes.txt file?

Twey
12-29-2007, 10:47 AM
Plaintext or HTML, each quote on a separate line.

Ryan Fitton
12-29-2007, 01:41 PM
can you give me an example please, becasue i have tried a new quote on a new line, but it is not working.:confused:

Master_script_maker
12-29-2007, 02:41 PM
from what i understand, you should open a plain text editor like notepad, each quote should be on a different line, then save it as quotes.txt

"something something something"
"blah blah blah"
"somethingelse somethingelse somethingelse"