Need a script to print "random" blocks of code.

Discussion in 'PHP' started by MhW, Jun 13, 2009.

  1. #1
    Hello everyone,

    I'm looking for (what should be) a simple PHP script. I'm fairly sure you're all familar with the "random quote" script that can be done, however can a similar thing be done with printing code on a web page?

    For example, say I have the following bits of code:
    <div class="red">This is red square</div>
    Code (markup):
    <div class="green">This is a green square</div>
    Code (markup):
    <div class="blue">This is a blue square</div>
    Code (markup):
    ...so, how could I use PHP to make those appear randomly each time the page is refreshed or reloaded?

    Thanks very much, and I'm happy to answer questions. :)
     
    MhW, Jun 13, 2009 IP
  2. webhed21

    webhed21 Member

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    
    <?php
    $random_num = rand(1,3);  // selects random number between 1 and 3
    
    switch ($random_num) {
        case 1:
            echo "<div class=\"red\">This is red square</div>";
            break;
        case 2:
            echo "<div class=\"green\">This is a green square</div>";
            break;
        case 3:
            echo "<div class=\"blue\">This is a blue square</div>";
            break;
    }
    ?>
    
    PHP:
     
    webhed21, Jun 13, 2009 IP
  3. MhW

    MhW Active Member

    Messages:
    370
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    85
    #3
    Cheers, works just fine. +Repped ;)
     
    MhW, Jun 13, 2009 IP
  4. webhed21

    webhed21 Member

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    glad i could help
     
    webhed21, Jun 13, 2009 IP