<?php 
// quiz.php by John Phillips on 6/5/2002 last revised 6/6/2002
// This file is brought into the main php program with an include statement.

// DESCRIPTION:
// Each item on each drop down menu gets its own set of quiz questions. The 
// questions are stored in an XML file. Each question appears within a
// <question> </question> tag and each answer appears immediately after
// the question within <answer> </answer> tags. This program reads in
// the appropriate quiz file and breaks it up into an array of questions. It
// arranges the questions into a random numeric sequence so that the student
// will not see the same pattern over and over. The random sequence is 
// stored in a cookie. 

// Retrieve the quiz file for the selected topic.

$fileName "quiz/{$menu}/{$item}.xml";

// Read in the quiz file (as an array) and then implode the array
// into a single string variable.
// Note: the @ signs suppress possible error messages.
 
$quiz = @implode('', @file($fileName));

// Use the PHP Perl-like regular expression to split the string on all
// <question> tags so that each array element will contain a single
// question along with its answer.

$matchesArray preg_split("/<question>/"$quiz);

// Check to see if a cookie exists for this quiz topic. If not then
// create a random number sequence. Otherwise, retrieve the cookie
// string and parse it on the commas with each number going into an 
// array called $numbers.

$currentQuizName $menu $item;
if( 
$cookieQuizName != $currentQuizName || $cookieQuestionOrder == null) {  
  
srand ((float) microtime() * 10000000);
  
$numQuestions count($matchesArray) - 1;
  
$numbers range(1$numQuestions);
  
shuffle($numbers);
}
else {
  
$numbers explode(","$cookieQuestionOrder);
}

// Grab one of the random numbers to determine the question we will use.

$questionNumber array_pop($numbers);

// Convert the random number list back into a string so that it can later
// be stored in a cookie. Note that each time we use a question number,
// that number is removed from the random number list. Therefore, the
// list grows smaller and smaller until it is null. A null list will
// trigger the creation of a new random number list in the section above. 

if( count($numbers) > ) {
  
$cookieQuestionOrder implode(","$numbers);
}
elseif( 
count($numbers) == ) {
  
$cookieQuestionOrder $numbers[0];
}
else {
  
$cookieQuestionOrder null;
}

// Here we split the question from its answer.

$matchesArray preg_split("/<\/question>[^<]*<answer>/"$matchesArray[$questionNumber]);

$question $matchesArray[0];

// Now we split off the </answer> tag leaving us with just the answer.

$matchesArray preg_split("/<\/answer>/"$matchesArray[1]);

// Keep in mind that this code will appear within the JavaScript heading/script section.
// This next bit of JavaScript code sets the correct answer variable so that JavaScript
// can get to it on the client side. It also, has JavaScript save our cookies.
?>

var correct = <?php echo '"' trim($matchesArray[0]) . '"'?>;

setCookie("cookieQuizName", "<?php echo $currentQuizName?>", null);
setCookie("cookieQuestionOrder", "<?php echo $cookieQuestionOrder?>", null);

<?php // end of quiz.php code ?>