I am currently using the code found on PHP quiz with on screen results which is grade.php:
<?php
$Questions = array(
1 => array(
'Question' => '1. CSS stands for',
'Answers' => array(
'A' => 'Computer Styled Sections',
'B' => 'Cascading Style Sheets',
'C' => 'Crazy Solid Shapes'
),
'CorrectAnswer' => 'B'
),
2 => array(
'Question' => '2. What is the Capital of the Philippines',
'Answers' => array(
'A' => 'Cebu City',
'B' => 'Davao City',
'C' => 'Manila City'
),
'CorrectAnswer' => 'C'
)
);
if (isset($_POST['answers'])){
$Answers = $_POST['answers']; // Get submitted answers.
// Now this is fun, automated question checking! ;)
foreach ($Questions as $QuestionNo => $Value){
// Echo the question
echo $Value['Question'].'<br />';
if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
echo 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
} else {
echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
echo 'You are correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; $counter++;
}
echo '<br /><hr>';
if ($counter=="")
{
$counter='0';
$results = "Your score: $counter/2";
}
else
{
$results = "Your score: $counter/2";
}
} echo $results;
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
<?php foreach ($Questions as $QuestionNo => $Value){ ?>
<h3><?php echo $Value['Question']; ?></h3>
<?php
foreach ($Value['Answers'] as $Letter => $Answer){
$Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
?>
<div>
<input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
<label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
</div>
<?php } ?>
<?php } ?>
<input type="submit" value="Submit Quiz" />
</form>
<?php
}
?>
The code works fine but I was wondering how am I able to display the questions randomly(To show 1 out of the 2 questions randomly)? I have tried array_rand(); but I am not able to implement that into these codes. Would appreciate any help I could get, thank you in advance!