HELP - Creating a PHP Quiz

Hi,

I am trying to create a quiz site using a multidimensional array and nested foreach loops. Right now, I am able to get all the questions and choices to appear with radio buttons, but the problem is that I can only check one button out of all of the five questions. How can I get it so that I can check one button for each question? (I know there needs to be more than one radio group, but I’m not really sure how to go about it)

Here is my current code:

[php]<?php

include(“includes/header.inc.php”);

?>

Test Your Knowledge

Choose the best answer to each question. Good luck!

<?php $questions = array( array( "question" => "1. What is...?", "choices" => array( "choice1" => "a", "choice2" => "b", "choice3" => "c", "choice4" => "d"), "correct" => "b", ), array( "question" => "2. What is...?", "choices" => array( "choice1" => "a", "choice2" => "b", "choice3" => "c", "choice4" => "d"), "correct" => "a", ), array( "question" => "3. What is...?", "choices" => array( "choice1" => "a", "choice2" => "b", "choice3" => "c", "choice4" => "d"), "correct" => "d", ), array( "question" => "4. What is...?", "choices" => array( "choice1" => "a", "choice2" => "b", "choice3" => "c", "choice4" => "d"), "correct" => "a", ), array( "question" => "5. What is...?", "choices" => array( "choice1" => "a", "choice2" => "b", "choice3" => "c", "choice4" => "d"), "correct" => "b", ) ); ?> <?php foreach($questions as $question){ echo $question['question'] . "
"; $choices = $question['choices']; foreach($choices as $choice){ echo '' . $choice . "

"; } } ?> <?php if(isset($_POST['submit'])){ if(!isset($_POST['choice'])){ echo 'Please choose an answer for each question'; } $finalScore = 0; $numberCorrect = 0; //insert code here to check for correct answers if ($_POST['choices'] == "a") { $finalScore += 20; $numberCorrect += 1; } if ($finalScore < 70){ $grade = "F"; } else if ($finalScore <= 75){ $grade = "D"; } else if ($finalScore <= 83){ $grade = "C"; } else if ($finalScore <= 92){ $grade = "B"; } else { $grade = "A"; } echo "You got " . $numberCorrect . " out of 5 answers correct.

"; echo "Your final score is " . $finalScore . "%" . " " . $grade . "

"; if ($grade == "A" or $grade == "B"){ echo "Good job!"; } else { echo "You need to try a little harder."; } } ?>
<?php

include(“includes/footer.inc.php”);

?>[/php]

First, please try to use php tags for it makes people looking at your code easier.

Some general observations, first try to have your gaming logic (at the top of the page or in a separate file) and HTML separate as possible though sometimes this is unavailable. While having your questions/answer in an array is perfectly fine this is where a database table comes in handy. :wink:

I have written a trivia game in PHP as well and I have also done it it flash. Here’s my HTML portion of my game:

[php]

<?php
$counter = 1;
foreach ($_SESSION[‘daily_questions’] as $key => $value) {
if (strlen($counter) < 2) {
$counter = “0” . $counter;
}
echo “<h1 class=“question”>” . $counter . ". " . $value[‘question’] . “\n”;
echo ‘

’ . $value[‘answer1’] . ‘’ . “
\n”;
echo ‘
’ . $value[‘answer2’] . ‘’ . “
\n”;
echo ‘
’ . $value[‘answer3’] . ‘’ . “
\n”;
echo ‘
’ . $value[‘answer4’] . ‘’ . “



\n”;
$counter += 1;
}
?>

[/php]

As you can see I did intermingle PHP and HTML, but the logic of the game is somewhere else (top of the page). Usually at the top of the page or on its own page. In the upcoming months I will be revising my trivia game as well. Getting back to your quiz this is how I handled the radio buttons, maybe this will give you some ideas on how to do it? I’m also using an array also though the questions/answers are pulled from a database table. Any questions I will be glad to help as best that I can and I’m sure others will be also willing to help.

Here’s the top of the page code:
[php]<?php
/*

  • Online Trivia Game Ver 1.83 Alpha
  • by John R Pepp
  • Start Date : 05-09-2015
  • Revised Date : 08-17-2015
    */
    require_once ‘lib/includes/utilities.inc.php’;

use website_project\trivia_game\QuizController as QuizController;
use website_project\trivia_game\Highscores as Scores;

$today = new DateTime(“Now”, new DateTimeZone(“America/Detroit”));
$day = new DateTime(“Now”, new DateTimeZone(“America/Detroit”));
$day->modify(“Today Midnight”);
/* Exit the user if he/she isn’t logged in */
if (!$user) {
header(“Location: login.php”);
exit();
}

$scores = new Scores($db, $user->id);
$data = $scores->read();

if (!$data) {
$data[‘user_id’] = $user->id;
$data[‘user_name’] = $user->username;
$data[‘score’] = 0;
$data[‘security_level’] = $user->security_level;
$data[‘started’] = $today->format(‘Y-m-d H:i:s’);
$data[‘finished’] = $today->format(“Y-m-d H:i:s”);
$data[‘date_played’] = $day->format(“Y-m-d H:i:s”);
$data[‘status’] = ‘no’;
$result = $scores->create($data);
}

$date1 = new DateTime($today->format(“Y-m-d H:i:s”), new DateTimeZone(“America/Detroit”)); // Today’s Date:

$date2 = new DateTime($data[‘date_played’], new DateTimeZone(“America/Detroit”)); // Date Played:
$diff = $date2->diff($date1); // Calculate the difference:
//echo “

” . print_r($diff,1) . “
\n”;
/* Re-open trivia game if more than 1 day /
if ($diff->d >= 1) {
/
Reset the variables to initial values /
$data[‘score’] = 0;
$data[‘date_played’] = $day->format(“Y-m-d H:i:s”);
$data[‘started’] = $today->format(“Y-m-d H:i:s”);
$data[‘finished’] = $today->format(“Y-m-d Hi:s”);
$data[‘status’] = ‘no’;
$scores->update($data);
}
/
Go To High Scores Table */
if ($data[‘status’] === ‘yes’) {
header(‘Location: high_scores.php’);
exit();
}

$quiz = new QuizController($db);

$status = $quiz->loadDailyQuestions(); // Load the daily questions inf from QuizController class:

if ($status === ‘new’) {
/* If $status is true then need to load new data of daily questions /
$_SESSION[‘daily_questions’] = $quiz->loadDailyQuestions();
} else {
/
if not then just load in the current day’s 10 questons */
$_SESSION[‘daily_questions’] = $status;
}

/* Grab the array of answers from user and put it in sessions */
$_SESSION[‘user_answer’] = filter_input(INPUT_POST, ‘answer’, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);

/* Only call the checkAnswer method if session array is set /
if (isset($_SESSION[‘user_answer’])) {
/
Call the actual method in order to check the answers against the database /
$_SESSION[‘display_results’] = $quiz->checkAnswers();
/
Unset the session array IMPORTANT! /
unset($_SESSION[‘user_answer’]);
/
redirect user to display results page */
header(“Location: display_results.php”);
exit();
}

require ‘lib/includes/header.inc.php’;
?>[/php]

I know most of it is probably mumble jumble, but it is just to show you that I do practice what I preach. ;D

Sponsor our Newsletter | Privacy Policy | Terms of Service