If all you want to do is shuffle the same question set around from student to student you can use the shuffle() function like so.
$shuffledArray = shuffle($array);
If you want to randomly select questions from your question set you can do one of the below options;
You can use the RAND() function in your query as @Anshuman Jaiswal suggested, but there is no guarantee that you will not get a duplicate question.
Here is a function that you can use for each student and guarantee that there will not be a duplicate question on a test. Plus, I added a parameter where you can select how many questions you want to pull from your question set.
function getRandomQuestions($results, $numberOfQuestions){
//Check to make sure you have a result.
if(count($results) == 0){
return FALSE;
}
//Check to make sure you have enough results for your desired amount of questions.
if(count($results) < $numberOfQuestions || $numberOfQuestions == 0){
$numberOfQuestions = count($results);
}
$randArray = array();
for($i = 0; $i < $numberOfQuestions; $i++){
$test = FALSE;
Do{
//Generate a basic random integer.
$rand = rand(0, count($results) - 1);
//Check to make sure the random number has not been used yet.
if(!in_array($rand, $randArray)){
$randArray[] = $rand;
$questionSet[] = $results[$rand];
$test = TRUE;
}
}while($test === FALSE);
}
return $questionSet;
}
$results = array(Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10);
$numTestQuestions = 5;
$questions = getRandomQuestions($results, $numTestQuestions);
print_r($questions);
This will output something like:
Array ( [0] => Q2 [1] => Q6 [2] => Q3 [3] => Q7 [4] => Q5 )
Updated Example
Using the code you have from above, this is all you need to do.
Add the function to the code, then run the queries, then call the function using the results from the queries.
function getRandomQuestions($results, $numberOfQuestions){ //<--The new function.
//Check to make sure you have a result.
if(count($results) == 0){
return FALSE;
}
//Check to make sure you have enough results for your desired amount of questions.
if(count($results) < $numberOfQuestions || $numberOfQuestions == 0){
$numberOfQuestions = count($results);
}
$randArray = array();
for($i = 0; $i < $numberOfQuestions; $i++){
$test = FALSE;
Do{
//Generate a basic random integer.
$rand = rand(0, count($results) - 1);
//Check to make sure the random number has not been used yet.
if(!in_array($rand, $randArray)){
$randArray[] = $rand;
$questionSet[] = $results[$rand];
$test = TRUE;
}
}while($test === FALSE);
}
return $questionSet;
}
if(isset($_SESSION['stdname']))
{
//Run your queries and get the results in r & r1.
$result=executeQuery("select stdanswer,answered from studentquestion where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
$r1=mysql_fetch_array($result);
$result=executeQuery("select * from question where testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
$r=mysql_fetch_array($result);
//Set the number of questions you want and then run the function.
$numTestQuestions = 5; //This will get 5 questions per student test.
$questions = getRandomQuestions($r1, $numTestQuestions);//<--Call Function.
var_dump($questions); //<---Display your results.
$numTestQuestions = 0; //If you set to zero if will display all test questions per student test.
$questions = getRandomQuestions($r, $numTestQuestions); //<--Call function.
var_dump($questions); //<--Display your results.
}