Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Display 3 Random Links

    Hi everyone!

    I have about 30 quotes and I want to display 3 of them at random. Can anyone help me with a PHP script that would be able to do this?

    Thanks

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    You could use PHP.

    The extension of your webpage would need to be .php (instead of .html).

    Say you have a list of quotes like this (text file is called "bumper_sticker.dat")
    http://www.catpin.com/bumper_sticker.dat

    Then, within your webpage (with the .php extension), you would have
    a script like this:

    PHP Code:
    <?php
    #Read Bumper Sticker File
    $filename="bumper_sticker.dat";
    $lines = array();
    $fp fopen($filename"r");
    $array1 explode("\n"fread($fpfilesize($filename)));
    fclose ($fp);

    #Generate Random Numbers
    srand(time());
    $arr = array();
    while ( 
    count($arr) < count($array1) ) {
    $x mt_rand(1,count($array1));
    if ( !
    in_array($x-1,$arr) ) { $arr[] = $x-1; }


    #Pick out the 3 Random Quotes
    $quote1 $array1[$arr[0]];
    $quote2 $array1[$arr[1]];
    $quote3 $array1[$arr[2]];

    echo 
    "<span style='font-family: helvetica; font-size: 9pt; color: #000000;'>".$quote1."</span><br />";
    echo 
    "<span style='font-family: helvetica; font-size: 9pt; color: #000000;'>".$quote2."</span><br />";
    echo 
    "<span style='font-family: helvetica; font-size: 9pt; color: #000000;'>".$quote3."</span><br />";
    ?>

    See my working test example:

    http://www.catpin.com/bumper_sticker3.php


    NOTE: you can make the script more compact, but I left it alone
    so you can more easily see how it works.




    .
    Last edited by mlseim; 11-09-2006 at 01:58 PM.

  • #3
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow thanks a lot!! that's exactly what I was looking for!

    ps. added rep

  • #4
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Or you just do it like this:
    PHP Code:
    <?php

    $quote 
    file("yourfile.txt");
    shuffle($quote);

    echo 
    "$quote[0]<br />\n$quote[1]<br />\n$quote[2]";

    ?>


  •  

    Posting Permissions

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