Well from your code there you pull across all the questions for a specific quiz... good. Now you just need to loop through the results and put the results into an array of questions along with their answers. Once you get them into an array then you can apply all sorts of functions on the array to mix up the questions into a random order. One way to do this is to create two random numbers (using rand()) and find those positions in the array and swap the values. Do this as many times as you like and in no time the array will be randomized.
Then just loop through the array to print the questions. Here is a demo for you...
CODE
<?php
$questions = array();
// Create our array of questions (which we fetched from the database)
$questions[0] = array("id" => 1,"question" => "Who is a better super hero, superman or spiderman?", "answer" => "superman");
$questions[1] = array("id" => 2,"question" => "Daredevil lacks what major sense?", "answer" => "sight");
$questions[2] = array("id" => 3,"question" => "When did Columbus discover america?", "answer" => "1492");
$questions[3] = array("id" => 4,"question" => "What is Avogadro's number?", "answer" => "6.0221415 x 10^23");
$questions[4] = array("id" => 5,"question" => "What is the tallest mountain in the world?", "answer" => "Mt. Everest");
// List the questions before our randomizing
echo "Before shuffling...<br/><br/>";
for ($i = 0; $i < 5; $i++) {
echo "Question is: " . $questions[$i]["question"] . "<br/>";
}
// Call our function to do some random swapping
mixup($questions);
// Show that they are mixed up
echo "<br/>After shuffling...<br/><br/>";
for ($i = 0; $i < 5; $i++) {
echo "Question is: " . $questions[$i]["question"] . "<br/>";
}
function mixup(&$arValues) {
// Do 30 random swaps
for ($i = 0; $i < 30; $i++) {
// Pick some random elements
$rand1 = rand(0,count($arValues) - 1);
$rand2 = rand(0,count($arValues) - 1);
// Swap them
$temp = $arValues[$rand1];
$arValues[$rand1] = $arValues[$rand2];
$arValues[$rand2] = $temp;
}
}
?>
It shows that we create some questions, put them into an array, randomize them up and then spit them out onto the page again in random order.
Or of course you can use the database to do the randomizing for you, but then you lack a bit of the customizability of determine how random they are etc. Either way will work.
Enjoy!
"At DIC we be array swapping code ninjas.... and that is ALL we swap."
This post has been edited by Martyr2: 22 May, 2008 - 02:27 PM