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.