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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Code to display DAILY random string?

    Hi guys, I've got a script that will display a random quote each time the page is visited or refreshed, but I'd like to have something that will pick a random string from a list and display it for the entire day...is this possible? Thanks!

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    it is possible indeed. How is your php knowledge?
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #3
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts
    I found this code, but I don't know how to use it?

    Code:
    function RandomQuoteByInterval($TimeBase, $QuotesArray){
     
        // Make sure it is a integer
        $TimeBase = intval($TimeBase);
     
        // How many items are in the array?
        $ItemCount = count($QuotesArray);
     
        // By using the modulus operator we get a pseudo
        // random index position that is between zero and the
        // maximal value (ItemCount)
        $RandomIndexPos = ($TimeBase % $ItemCount);
     
        // Now return the random array element
        return $QuotesArray[$RandomIndexPos];
    }

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php

    function RandomQuoteByInterval($TimeBase$QuotesArray){
     
        
    // Make sure it is a integer
        
    $TimeBase intval($TimeBase);
     
        
    // How many items are in the array?
        
    $ItemCount count($QuotesArray);
     
        
    // By using the modulus operator we get a pseudo
        // random index position that is between zero and the
        // maximal value (ItemCount)
        
    $RandomIndexPos = ($TimeBase $ItemCount);
     
        
    // Now return the random array element
        
    return $QuotesArray[$RandomIndexPos];
    }

    $QuotesArray = array("Have a great day!""HAHAHAHHA looser""You smell like a bed of roses");
    echo 
    RandomQuoteByInterval(1440$QuotesArray);

    ?>
    should do the trick
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    Iconoclast (10-18-2009)

  • #5
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Works perfectly, thanks so much mate!

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    Dont thank me, thank the bloke who wrote the code!
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #7
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Hey, sorry, now I have another question.

    It seemed to be working, but it doesn't seem to cycle through them every 24 hours.

    It has something to do with this line here:
    Code:
    echo RandomQuoteByInterval(1440, $QuotesArray);
    I've tried substituting various values in, but so far nothing really seems right. Either way, even when I use a really small number like 0.000000001, it doesn't seem to cycle through them (and if it is, then the timer is REALLY long before it changes to the next one).

    I'm assuming 1440 is because of the # of minutes in a day, but what if that's not what it's being measured by? Any ideas?

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Here is that function and example:
    http://www.jonasjohn.de/snippets/php...dom-quotes.htm

    So wherever 1440 came from, it should simply be the day of the year... not very random.

    You may need to employ a database or caching. Let's go back to square one: How are your quotes stored?

  • #9
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts
    I just used an array, like so:

    Code:
    function RandomQuoteByInterval($TimeBase, $QuotesArray){
     
        // Make sure it is a integer
        $TimeBase = intval($TimeBase);
     
        // How many items are in the array?
        $ItemCount = count($QuotesArray);
     
        // By using the modulus operator we get a pseudo
        // random index position that is between zero and the
        // maximal value (ItemCount)
        $RandomIndexPos = ($TimeBase % $ItemCount);
     
        // Now return the random array element
        return $QuotesArray[$RandomIndexPos];
    }
    
    $QuotesArray = array("Phoenix",
    "30 Seconds To Mars",
    "The Beatles",
    "Rise Against",
    "The Spill Canvas",
    "The Killers",
    "Metric",
    "Aerosmith");
    echo RandomQuoteByInterval(1440, $QuotesArray);
    It's actually for a Band/Artist of the day. I've got way more in that, I just shortened it to a few so you would get the idea. Does this help?

  • #10
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    just wrote you this....yawn 3 hours kip was nice...

    PHP Code:
    <?php
    //http://www.actwebdesigns.co.uk
    //luke@actwebdesigns.co.uk

    function RandomQuoteByInterval($howOften$QuotesArray){
     
        if(
    file_exists("randomQuote/data.txt"))
        {
            
    $handle fopen("randomQuote/data.txt"'r');
            
    $lines file("randomQuote/data.txt");
            
    $hash trim($lines[0]);
            
    $current trim($lines[1]);
            
    fclose($handle);
            
            
    $trueHash md5($current.floor((time()/(($howOften)*60))));
            if(
    $hash!=$trueHash)
            {
                while(
    true)
                {
                    
    $randomKey rand(0, (count($QuotesArray)-1));
                    if(
    $QuotesArray[$randomKey]!=$current)
                    {
                        if(
    $fp = @fopen("randomQuote/data.txt"'w'))
                        {
                            
    fwrite($fp$trueHash."\n".$QuotesArray[$randomKey]);
                            
    fclose($fp);
                            return 
    $QuotesArray[$randomKey];
                        }
                        else
                        {
                            die(
    "Error: problems opening data.txt");     
                        }
                        break;
                    }
                }            
            }
            else
            {
                return 
    $current;    
            }
        }
        else
        {
            if(!
    file_exists("randomQuote/"))
            {
                
    mkdir("randomQuote/"0700);
            }
            
            
    $randomKey rand(0, (count($QuotesArray)-1));
            
    $trueHash md5($QuotesArray[$randomKey].floor((time()/(($howOften)*60))));
            if(
    $fp = @fopen("randomQuote/data.txt"'w'))
            {
                
    fwrite($fp$trueHash."\n".$QuotesArray[$randomKey]);
                
    fclose($fp);
                return 
    $QuotesArray[$randomKey];
            }
            else
            {
                die(
    "Error: problems opening data.txt");     
            }        
        }
    }

    $QuotesArray = array("Phoenix",
    "30 Seconds To Mars",
    "The Beatles",
    "Rise Against",
    "The Spill Canvas",
    "The Killers",
    "Metric",
    "Aerosmith");

    echo 
    RandomQuoteByInterval(1440$QuotesArray); // add howOften in minutes 1440 = 24*60

    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    Iconoclast (10-20-2009)

  • #11
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Wow thanks mate...but everytime I refresh the page, it displays a new artist/band from the array? Even though the value is set to 1440?

    I had to manually create the directory called randomQuote, as well as data.txt and chmod permissions to 777.

    One question though I'm kind of confused, what exactly is the data.txt file for? What is being written to that file? I checked it and it's got a long string of random characters, then on the next line down is the artist that comes up as the one of the day on my page. What's that for?

    Thanks again mate! +thanks

  • #12
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts
    ^^Any ideas?

  • #13
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,374
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    Iconoclast ...

    What do you consider "a day"?
    Is that based on YOUR timezone?
    Or your server timezone?
    Or your visitor's timezone?

    For me, the next day (Wed) starts in about 5 hours.
    For my friend in Australia, it's already Wed, 10AM ...
    So, which quote will he see?

    You need to either pick one timezone to use, or use cookies and IP
    address to attempt to pick the user's timezone. The internet makes
    the world very small, but time is very abstract.

  • #14
    Banned
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Iconoclast View Post
    Hey, sorry, now I have another question.

    It seemed to be working, but it doesn't seem to cycle through them every 24 hours.

    It has something to do with this line here:
    Code:
    echo RandomQuoteByInterval(1440, $QuotesArray);
    I've tried substituting various values in, but so far nothing really seems right. Either way, even when I use a really small number like 0.000000001, it doesn't seem to cycle through them (and if it is, then the timer is REALLY long before it changes to the next one).

    I'm assuming 1440 is because of the # of minutes in a day, but what if that's not what it's being measured by? Any ideas?
    This is an interesting discussion. thank you for sharing
    However I think you should vary more examples to your writing much more interesting !
    _________________________
    Faire simulation pret personnel en ligne | Faire calcul taux pret personnel en ligne | Demander un financement simulation credit personnel

  • #15
    New Coder
    Join Date
    Dec 2008
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Iconoclast ...

    What do you consider "a day"?
    Is that based on YOUR timezone?
    Or your server timezone?
    Or your visitor's timezone?

    For me, the next day (Wed) starts in about 5 hours.
    For my friend in Australia, it's already Wed, 10AM ...
    So, which quote will he see?

    You need to either pick one timezone to use, or use cookies and IP
    address to attempt to pick the user's timezone. The internet makes
    the world very small, but time is very abstract.
    Well, my timezone is GMT - 5 hours. So I would be happy using that. But I'd settle for whichever method is easiest, provided that it changes every 24 hours instead of each time the page is refreshed.

    Just as a side note, would I not be able to somehow use the server's time? Thanks.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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