• 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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Maybe you can look how this plugin works and change it for what you need

    Thread Starter J B

    (@kreuzian)

    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?

    Thread Starter J B

    (@kreuzian)

    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).

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

    Thread Starter J B

    (@kreuzian)

    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?

    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Randomized, but static, output on every page? Possible? How?’ is closed to new replies.