I need something to rotate...

Discussion in 'PHP' started by Peter Brown, Apr 29, 2007.

  1. #1
    Hi there,
    I basically need something to rotate a few "questions".

    So when you load the page it will show you one random question and when you refresh it will show you a different one. I need to rotate about 5 questions.

    Anyone know how I could do this?

    Thanks in advance,
    Peter Brown
     
    Peter Brown, Apr 29, 2007 IP
  2. Monty

    Monty Peon

    Messages:
    1,363
    Likes Received:
    132
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Something like that should work :

    
    <?
    $question = array('question1' ,'question2',...,'question5');
    $rand = mt_rand(0, count($question)-1);
    echo $question[$rand];
    ?>
    PHP:
    It will randomly pull a question from the array.
     
    Monty, Apr 29, 2007 IP
    Peter Brown likes this.
  3. Peter Brown

    Peter Brown Well-Known Member

    Messages:
    1,638
    Likes Received:
    117
    Best Answers:
    0
    Trophy Points:
    180
    #3
    Thanks a lot! It worked :D

    Rep added :D

    Thanks again! :D
     
    Peter Brown, Apr 29, 2007 IP
    YIAM likes this.
  4. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #4
    Edit:

    Monty allready posted the solution so, deleted my code.
     
    YIAM, Apr 29, 2007 IP
    Peter Brown likes this.
  5. Codythebest

    Codythebest Notable Member

    Messages:
    5,764
    Likes Received:
    253
    Best Answers:
    0
    Trophy Points:
    275
    #5
    Monty,
    I have some include in my root: 1.inc.php , 2.inc.php , etc...
    Can I go:
    <php?
    $include = array('1.inc.php code' ,'2.inc.php code' ,...,);
    $rand = mt_rand(0, count($include)-1);
    echo $include[$rand];
    ?>

    and instead of the code, how can I call the includes?
     
    Codythebest, Apr 30, 2007 IP
  6. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    $first = 1;
    $last = 7;
    include( rand($first, $last) . '.inc.php');
    
    PHP:
     
    decepti0n, Apr 30, 2007 IP
    Codythebest likes this.
  7. Codythebest

    Codythebest Notable Member

    Messages:
    5,764
    Likes Received:
    253
    Best Answers:
    0
    Trophy Points:
    275
    #7
    My mistake. Thought rand could go more than 2 variables. Didn't check deeper. Green diserved...
     
    Codythebest, Apr 30, 2007 IP