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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Newbie, I need help

    I'm trying to make a php file that puts random quotes on my webpage. I have a txt file full of quotes, and they're all separated by slashes ( / ) but I'm still having trouble. Each day I want a random quote to pop up. You know, like today, a George Washington quote, next day, another quote, etc. Here's what I have so far, I was wondering if anyone could tell me what I need to fix or fill in. Thanks I appreciate it!

    -Lucky

    Here's my coding so far (I really have no experience in PHP whatsoever, so if this can be explained as simply as possible I'd appreciate it!):

    <?php
    $today_counter = 3;
    $fh = fopen("quotes.txt", "r");
    $counter = 0;
    while (!feof($fh) && $counter < $today_counter) {
    $line = fgets($fh, 1024);
    if (substr($line,0,1) != "#") {
    $counter++;
    }
    }
    fclose($fh);
    list($person,$date,$quote) = split("/", $line);
    print "Quote of the Day:\n";
    print "Person: $person\n";
    print "Date: $date\n";
    print "Quote: $quote\n";

    ?>

    <?php
    $textfile = "Includes/Quotes.txt"; //quotes file
    if ($quotes = @file("$textfile")) { //don't display errors on file open
    echo $quotes[array_rand($quotes)]; //echo a random quote
    }else{
    echo ("default quote"); //if quotes file wasn't found, echo out a default quote
    }
    ?>

    (Something a friend sent me)

    set the permisions on your txt file to 777.
    <?php
    $file = 'nameof file.txt';
    $array = file($file);
    shuffle($array);
    echo "$array[0]";
    ?>

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,043
    Thanks
    19
    Thanked 42 Times in 42 Posts
    whats happening or not happening with you current code?

  • #3
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Why not just save yourself some time and use a javascript or similar that means you don't have to re-invent the wheel?
    PHP Code:
    function initBanner() {
        if ( !
    document.getElementById ) return;
        
    // arguments: id, delay (amount of time in milliseconds you linger on each item)
        // set up pause onmouseover and resume onmouseout? (boolean)
    var ban1 = new dw_Banner('bannerDiv'4500true);
        
    // add as many items as you like
        
    ban1.addItem('‘Do what you love and love what your’re doing and you’ll never work another day in your life’ Unknown source');
        
    ban1.addItem('‘If you are not using your smile, your’e like a man with a million dollars in the bank account and no checkbook’ Les Giblin');
        
    ban1.addItem('‘The time is always right to do what is right’ Martin Luther King Jr');
        
    ban1.addItem('‘What lies behind us and what lies before us are tiny matters compared to what lies within us’ Ralph Waldo Emerson');
        
    ban1.addItem('‘Integrity is not a 90 percent thing, not a 95 percent thing, either you have it or you don’t’ Peter Scotese');
        
    ban1.addItem('‘Never, never, never give up’ Winston Churchill');
        
    ban1.addItem('‘You must be the change you wish to see in the world’ Ghandi');
    ban1.rotate(); // Begin the rotation

    Then add this to the body tag!
    PHP Code:
    <body onLoad="initBanner()" 
    Add your div wherever!
    PHP Code:
    <div id="bannerDiv" >
    ‘Nevernevernever give up’
    <br>
    Winston Churchill
    </div
    Now look I did not create this so credit goes to: www.dyn-web.com/bus/terms.html
    Check their terms and stuff but I am sure it's ok

    Have fun
    Thanks for you support!

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    nearly there..

    PHP Code:
    $file 'nameoffile.txt';
    $array file($file);
    shuffle($array);
    $quotes explode('/',$array[0]);
    shuffle($quotes);
    echo 
    $quotes[0]; 
    you may want to remove the lines starting with '#" unless they have a particular purpose ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $file file_get_contents('quotes.txt');
    $array explode("/"$file);
    $array array_rand($array1);
    echo 
    $array[0]; 


  •  

    Posting Permissions

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