Random Rotation Script

Discussion in 'PHP' started by John84, Feb 12, 2007.

  1. #1
    Hello,

    I have used the following php script to get random text in my website...

    <?php
    $number=mt_rand(1, 6);
    
    if ($number=="1") {
    
    include "navrandom1.php";
    
    }
    
    if ($number=="2") {
    
    include "navrandom2.php";
    
    }
    
    if ($number=="3") {
    
    include "navrandom3.php";
    
    }
    
    if ($number=="4") {
    
    include "navrandom4.php";
    
    }
    
    if ($number=="5") {
    
    include "navrandom5.php";
    
    }
    
    if ($number=="6") {
    
    include "navrandom6.php";
    
    }
    ?>
    
    HTML:
    ... each .php page is simply a paragraph and it works just fine, however, I was hoping to add about 20 different items as oppose to the current 6. Is there a shortcut in doing this or do I have to put...

    [HTML]<?php
    if ($number=="?") {
    
    include "navrandom?.php";
    
    }
    HTML:
    for each single item. Also, is there a way to put all the paragraphs in one page and have the script randomly choose one paragraph to display?
     
    John84, Feb 12, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    include("navrandom{$number}.php");
    
    
    PHP:
     
    nico_swd, Feb 12, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    You could use arrays.

    
    
    $paragraphs = array(
        '<p>Paragraph 1</p>',
        '<p>Paragraph 2</p>',
        '<p>Paragraph 3</p>',
        '<p>Paragraph 4</p>',
    );
    
    $random = $paragraphs[array_rand($paragraphs)];
    
    echo $random;
    
    
    PHP:
     
    nico_swd, Feb 12, 2007 IP
    John84 likes this.
  4. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #4
    edit:
    better options have been suggested
     
    papa_face, Feb 12, 2007 IP
  5. John84

    John84 Active Member

    Messages:
    1,288
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Works beautifully. Thanks a bunch!
     
    John84, Feb 12, 2007 IP