WordPress.org

Ready to get started?Download WordPress

Forums

Randomized, but static, output on every page? Possible? How? (7 posts)

  1. J B
    Member
    Posted 3 years ago #

    I'd like to echo via PHP or wordpress, some sort of randomized code for every page on a 1,000+ page site. Like a different quote on every page, but for it to be STATIC so when it reloads it doesn't change.

    Is this possible? Can someone write me an example code for me to use?

    Thank you for your time.

  2. xdseth
    Member
    Posted 3 years ago #

    Maybe you can look how this plugin works and change it for what you need

  3. J B
    Member
    Posted 3 years ago #

    I have no idea how I would modify that or how it works.

    I thought there might be some wordpress related solution or php solution such as the one below

    <?
     //Chooses a random number
     $num = Rand (1,6);
     //Based on the random number, gives a quote
     switch ($num)
     {
     case 1:
     echo "Time is money";
     break;
     case 2:
     echo "An apple a day keeps the doctor away";
     break;
     case 3:
     echo "Elmo loves dorthy";
     break;
     case 4:
     echo "Off to see the wizard";
     break;
     case 5:
     echo "Tomorrow is another day";
     break;
     case 6:
     echo "PHP is cool!";
     }
     ?>

    But it always changes on refresh. I want it to NOT change on refresh. Is this possible?

  4. J B
    Member
    Posted 3 years ago #

    I found one solution, by adding
    srand(floor(time() / (60*60*24)));
    before the rand it stays the same for 24 hours for example (and I guess I can set it to a super high time to avoid repeating).

    HOWEVER, if put in the sidebar or header it outputs the same result for ALL pages. Is there any way I can put it in the header and have it show a different result for EACH page? I suspect I will need to use wordpress code for it (adding it into the loop).

  5. kihjin
    Member
    Posted 3 years ago #

    How about generating the random string and saving it in a cookie?

  6. J B
    Member
    Posted 3 years ago #

    The goal is that every user who sees Page A sees one quote,
    And every user who sees Page B sees a different quote.

    Thus randomized, but not changing (neither on refresh or per user).
    Any ideas?

  7. maxk
    Member
    Posted 3 years ago #

    Here's a simple solution based on the page URL + parameters:

    <?php
    
    // First convert the request URI to a hexadecimal number, via the md5 hash
    $num = md5($_SERVER['REQUEST_URI']);
    
    // Next, shorten it so the number isn't too big, and convert it to decimal
    $num = hexdec(substr($num, 27));  // this gives us a number between 0 and 65,535
    
    // Now, convert it to a range between 0 and 5 by taking the modulus
    $num = $num % 6;
    
    // Add one...
    $num++;
    
    // And use the same switch statement we used before to achieve our desired result...
    switch ($num) {
      case 1:
        echo "Time is money";
        break;
      case 2:
        echo "An apple a day keeps the doctor away";
        break;
      case 3:
        echo "Elmo loves dorthy";
        break;
      case 4:
        echo "Off to see the wizard";
        break;
      case 5:
        echo "Tomorrow is another day";
        break;
      case 6:
        echo "PHP is cool!";
    }
    ?>

    This will be different from page to page, but stay the same on the same page.

Topic Closed

This topic has been closed to new replies.

About this Topic