Results 1 to 4 of 4

Thread: php random quote or another way?

  1. #1
    Ian Shere
    Guest

    Default php random quote or another way?

    Found a great little piece of coding that I'd like to use (I basically
    wanted a random quote popping up on my homepage). What I found was a piece
    of PHP coding but I cannot figure out how to use it on my page. I have
    tried inserting HTML here there and everywhere but nothing shows up when I
    run the page (even publishing remotely). Code is as follows:

    <?php
    $quotes = array(
    'Impossible is a word to be found only in the
    dictionary of fools.<br />&mdash; Napoleon Bonaparte',

    'A person who never made a mistake never tried any
    thing new.<br /> &mdash; Albert Einstein',

    'Show me a thoroughly satisfied man and I will show
    you a failure.<br />&mdash; Thomas A. Edison',
    );

    /*
    generate a random number between zero
    and the number of quotes minus one
    */
    $rand = rand(0, (count($quotes)-1));

    // output the random quote
    echo $quotes[$rand];
    ?>

    What am I doing wrong? Or does anyone have a better/easier way to do this?
    JS maybe?

    TIA

    Ian


  2. #2
    TheBox
    Guest

    Default Re: php random quote or another way?

    Hi Ian,

    I use this little script:

    <?
    // Quick random quote
    // This script reads a quote file (flat text file), and picks one of the
    loaded
    // quotes at random and displays it.
    // Line breaks and formatting can be put into the quotefile as HTML tags
    // Place the quotes.txt on your site, edit the $quotefile variable below,
    // And copy and paste this code into your PHP page.

    // $delim tells this script what delimits the quotes (default is a CR/LF)
    $delim = "\n";

    // $wuotefile points to the file that holds the actual quotes
    $quotefile = "quotes.txt";

    $fp = fopen($quotefile, "r");
    $contents = fread($fp, filesize($quotefile));
    $quote_arr = explode($delim,$contents);
    fclose($fp);
    srand((double)microtime()*1000000);
    $quote_index = (rand(1, sizeof($quote_arr)) - 1);
    $herequote = $quote_arr[$quote_index];
    echo $herequote;
    ?>

    The way I implement is to place a text box, ctrl+t to insert the above
    script. Size it to your liking. Then create the quotes.txt file with all
    your quotes in it. Upload the txt file to the same directory as the page.
    Publish your page. Make sure your page extension is .php.
    -
    HTH,
    Bert


    "Ian Shere" <ian@tarzandesign.co.nz> wrote in message
    news:g2s0th$oi34@flsun90netnews01.netobjects.com.. .
    > Found a great little piece of coding that I'd like to use (I basically
    > wanted a random quote popping up on my homepage). What I found was a
    > piece of PHP coding but I cannot figure out how to use it on my page. I
    > have tried inserting HTML here there and everywhere but nothing shows up
    > when I run the page (even publishing remotely). Code is as follows:
    >
    > <?php
    > $quotes = array(
    > 'Impossible is a word to be found only in the
    > dictionary of fools.<br />&mdash; Napoleon Bonaparte',
    >
    > 'A person who never made a mistake never tried any
    > thing new.<br /> &mdash; Albert Einstein',
    >
    > 'Show me a thoroughly satisfied man and I will show
    > you a failure.<br />&mdash; Thomas A. Edison',
    > );
    >
    > /*
    > generate a random number between zero
    > and the number of quotes minus one
    > */
    > $rand = rand(0, (count($quotes)-1));
    >
    > // output the random quote
    > echo $quotes[$rand];
    > ?>
    >
    > What am I doing wrong? Or does anyone have a better/easier way to do
    > this? JS maybe?
    >
    > TIA
    >
    > Ian




  3. #3
    =?iso-8859-1?Q?Chuck_=ABBeyondFusion=BB?=
    Guest

    Default Re: php random quote or another way?

    Did you publish the page with a .php file extension?

    pagename.php rather than pagename.html

    As Bert said, you use a Ctrl+T in a text box and then position the text box
    where you want the script to execute.
    --
    Chuck Joslin
    BeyondFusion.com - Your Fusion Community
    www.beyondfusion.com

    Register domain names at www.awavedomains.com

    "Ian Shere" <ian@tarzandesign.co.nz> wrote in message
    news:g2s0th$oi34@flsun90netnews01.netobjects.com.. .
    > Found a great little piece of coding that I'd like to use (I basically
    > wanted a random quote popping up on my homepage). What I found was a
    > piece of PHP coding but I cannot figure out how to use it on my page. I
    > have tried inserting HTML here there and everywhere but nothing shows up
    > when I run the page (even publishing remotely). Code is as follows:
    >
    > <?php
    > $quotes = array(
    > 'Impossible is a word to be found only in the
    > dictionary of fools.<br />&mdash; Napoleon Bonaparte',
    >
    > 'A person who never made a mistake never tried any
    > thing new.<br /> &mdash; Albert Einstein',
    >
    > 'Show me a thoroughly satisfied man and I will show
    > you a failure.<br />&mdash; Thomas A. Edison',
    > );
    >
    > /*
    > generate a random number between zero
    > and the number of quotes minus one
    > */
    > $rand = rand(0, (count($quotes)-1));
    >
    > // output the random quote
    > echo $quotes[$rand];
    > ?>
    >
    > What am I doing wrong? Or does anyone have a better/easier way to do
    > this? JS maybe?
    >
    > TIA
    >
    > Ian



  4. #4
    Ian Shere
    Guest

    Default Re: php random quote or another way?

    That was the problem Chuck - like I said, I'm a real dabbler and "a little
    knowledge is dangerous!!!" Didn't realise I needed to publlish as *.php
    pages! Doh, seems logical now I know! :-)

    Cheers

    Ian

    "Ian Shere" <ian@tarzandesign.co.nz> wrote in message
    news:g2s0th$oi34@flsun90netnews01.netobjects.com.. .
    > Found a great little piece of coding that I'd like to use (I basically
    > wanted a random quote popping up on my homepage). What I found was a
    > piece of PHP coding but I cannot figure out how to use it on my page. I
    > have tried inserting HTML here there and everywhere but nothing shows up
    > when I run the page (even publishing remotely). Code is as follows:
    >
    > <?php
    > $quotes = array(
    > 'Impossible is a word to be found only in the
    > dictionary of fools.<br />&mdash; Napoleon Bonaparte',
    >
    > 'A person who never made a mistake never tried any
    > thing new.<br /> &mdash; Albert Einstein',
    >
    > 'Show me a thoroughly satisfied man and I will show
    > you a failure.<br />&mdash; Thomas A. Edison',
    > );
    >
    > /*
    > generate a random number between zero
    > and the number of quotes minus one
    > */
    > $rand = rand(0, (count($quotes)-1));
    >
    > // output the random quote
    > echo $quotes[$rand];
    > ?>
    >
    > What am I doing wrong? Or does anyone have a better/easier way to do
    > this? JS maybe?
    >
    > TIA
    >
    > Ian



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •