Results 1 to 2 of 2

Thread: View Randomize Option with Next and Back buttons

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    266

    Question View Randomize Option with Next and Back buttons

    Hi,

    I have a problem. In the following code I would like to randomize 5 questions out of 10 and display it one by one by clicking 'Next' button. In addition, I like to see the previous question by clicking 'Back' button.

    Problem is: questions are not coming properly when I toggle between Back and Next buttons. They are providing different result in different clicking. Why this is happening ? Please help.

    PHP Code:

    <?php

    $question
    =array(
                        
    => "Question 01",
                        
    => "Question 02",
                        
    => "Question 03",
                        
    => "Question 04",
                        
    => "Question 05",
                        
    => "Question 06",
                        
    => "Question 07",
                        
    => "Question 08",
                        
    => "Question 09",
                        
    => "Question 10"
                
    );

    $answer=array(
                        
    => "Answer 01",
                        
    => "Answer 02",
                        
    => "Answer 03",
                        
    => "Answer 04",
                        
    => "Answer 05",
                        
    => "Answer 06",
                        
    => "Answer 07",
                        
    => "Answer 08",
                        
    => "Answer 09",
                        
    => "Answer 10"
                
    );

    $id=array(0,1,2,3,4,5,6,7,8,9);

    shuffle($id);

    session_start();

    $_SESSION["count"] = 0;

    if(isset(
    $_POST["back"]))
    {
        if(
    $_SESSION["count"] > 0)
            
    $_SESSION["count"] = $_SESSION["count"] - 1;
    }

    if(isset(
    $_POST["next"]))
    {
        
    $_SESSION["count"] = $_SESSION["count"] + 1;
        if(
    $_SESSION["count"] > 4)
            
    header("Location:result.php");
    }

    ?>

    <html>
    <head>
    <title>Test Random Question</title>
    </head>
    <body>
    <form name="f" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    <p align="center">
    <?php

        
    echo $question[$id[$_SESSION["count"]]];

    ?>
    </p>
    <p align="center">
    <input type="submit" id="back" value="Back">&nbsp;
    <input type="submit" id="next" value="Next">
    </p>
    </form>
    </body>
    </html>
    I will be waiting for the reply. Thanks a lot.

  2. #2
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Re: View Randomize Option with Next and Back buttons

    When you say not properly do you mean that you start at 6 go forwards to 9 and then back to 2?

    (say)

    This is because the random order is not preserved across page calls. You shuffle the order every time. You could put a list of five questions into session data

    PHP Code:
    $_SESSION['questionlist'] = array(1,9,5,7,3); 
    Then you would have some form of preservation of order...

    Is that what you are after?
    ?
    'What's this bit for anyway?
    For Jono

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width