You are using a browser which is not compatible with CSS (for more information, see Tara's tutorial). Because of this, it is possible that our website may not appear correctly in your browser. We apologise for the inconvenience, and recommend you upgrade your browser to one which is compatible with CSS. For more information, please visit our Browser Upgrade page.

4WebHelp

Scripts: PHP: Random Quote

This script will output a random quote.

Demo

<?php

###########################################################################
#                                                                         #
# Copyright © http://www.4webhelp.net/                                    #
# Neither http://www.4webhelp.net/ nor its members accept any             #
# responsibility, either expressed or implied, for any damage caused by   #
# using this script or the misuse of this script.                         #
#                                                                         #
#                                                                         #
#                          INSTRUCTIONS                                   #
#                                                                         #
# 1) Copy this code to an editor such as Notepad and save it with a       #
# .php  extension.                                                        #
# 2) FTP this file to a folder on your site in ASCII mode                 #
# 3) Add your quotes - ask in our forums if you don't know how to         #
# 4) Call up this file in your web browser to see a quote                 #
#                                                                         #
###########################################################################

  $quotes[] = 'Your first quote';
  $quotes[] = 'Your second quote';
  $quotes[] = 'etc...';

  srand ((double) microtime() * 1000000);
  $random_number = rand(0,count($quotes)-1);

  echo ($quotes[$random_number]);

?>

<p>This script courtesy of <a href="http://www.4webhelp.net/">4WebHelp</a>.</p>
Latest comments
bibi
I tried   mawst' version
<? $r_array=file('quotes.txt');
shuffle($r_array);
echo $mQuotePath[0];
echo $r_array[0];
?>

had to eliminate  the first echo line.  After that, it worked well.
Chrisean
Hi Im Using this for the random quotes:

<?php
$quote=file("your-quotes.txt");
$totalnumberofquotes=count($quote);
$rdm=rand(1,$totalnumberofquotes);
echo $quote[$rdm];
?>

Just make sure that your external .txt file is in the same path as you .php file
AlEloo
I want ask if you can edit that code to be random every one day or time I specify it. Also, If you can make a code to include specify text for each day, that will be a nice idea. Very Happy




Thanks a lot
Kind Regards,
phpnerd
This script is REALLY inefficient....

Try using it on a page, then benchmark the page with ab (apache benchmark)....

you wont wanna use it if your site is high traffic.
Cthos
There are two ways to get around the problem with  a ' causing problems in a script.

The first one is to instead use double quotes when making a quote as shown by:

$quotes[] = "This is Steve's llama.";

The other way is to use single quotes and to escape the internal 's with a backslash:

$quotes[] = 'This is Steve\'s llama.';

Hope that helps.
Dargel Napoles
I'm new to php, but when I try to run it using the require() it gives me a parsing error.
nuc
how do i insert this in an html website?
mawst
Just take out the mQuotePath bit. i was using it for debugging. Whoops.
mawst
Here's what everyone (I think) was asking for. A 3 line script that uses a flatfile for quotes.


Make a file called quotes.txt

Put each quote on it's on line.

Here's a script:

<?
$r_array=file('quotes.txt');
shuffle($r_array);
echo $mQuotePath[0];
echo $r_array[0];
?>
Arjay
Can someone tell me if there's a variation out there that I can get in order to put a quote on my website and have it change every day, not on every browser reload?

Also, I need a bit of help on how to incorporated it into my website since I have almost zero knowlege with html LOL

If someone could help me I'd really appreciate it!  

My email is: atbdoorinc @ rogers.com ((remove spaces to reply))

My Site: http://www.picurkamonkey.com/index.html

Thank you!
Daniel, 4WebHelp Team Member
Sure! Simply replace
 $quotes[] = 'Your first quote';
 $quotes[] = 'Your second quote';
 $quotes[] = 'etc...';
with
include('name_of_your_text_file.txt');
And put this in your text file:
<?php  $quotes[] = 'Your first quote';
 $quotes[] = 'Your second quote';
 $quotes[] = 'etc...';
?>
Rob
Would it be possible to use an include to keep the quotes in a seperate file?
Daniel, 4WebHelp Team Member
$quotes[$random_number] contains the quote, so just remove the echo function line, and do what you want with the variable I mentioned above!
Mav
How could you incorporate that into and existing php file so you could just use the script in another area instead of it being printed directly using the echo.  I would like to be able to call the script from other places in a code I already have.
Daniel, 4WebHelp Team Member
Many thanks for pointing that out!
SfCommand
There appear to be 2 opening <?php tags. Shouldn't either the second one be removed, or the first one closed?
Tim
Brilliant, worked a treat!

Add a new comment

This page is © Copyright 2002-2024, 4WebHelp. It may not be reproduced without 4WebHelp's prior permission.