I’m trying to make a simple three question quiz in php. After the user signs in, they begin the quiz and have 15 minutes to complete it (I have the session part figured out). One true/false, one multiple choice and one short answer. Each question appears separately on each page.
My question is whether my logic is right so far. This program will not run due to several syntax errors. It tells me to delete brackets when they should be there. For example, in line 55, it tells me I should delete the last }
if ($selected_radio == 'false') {
correct()
}
Could someone look over this and give me some pointers on where to go? If I’m completely off track, please let me know how I should move forward. Examples would be great.
I appreciate your time. Since this is rather long, I uploaded the code here: http://paste.bradleygill.com/index.php?paste_id=339313
My code:
<?php
ini_set('session.gc_maxlifetime',900);
ini_set('session.gc_probability',1);
ini_set('session.gc_divisor',1);
//echo ini_get("session.gc_maxlifetime");
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}
if (!session_is_registered("number"))
{
$_SESSION["number"] = 0;
$_SESSION["answer"] = 0;
$_SESSION["correct"] = 0;
}
$total_number = 3;
print <<<TOP
<html>
<head>
<title> History Quiz </title>
</head>
<body>
<h3> History Quiz </h3>
TOP;
$number = $_SESSION["number"];
$answer = $_SESSION["answer"];
$correct = $_SESSION["correct"];
if ($number == 0){
$answer = "false";
$_SESSION["answer"] = $answer;
print <<<FIRST
<p> You will be given $total_number questions in this quiz. <br /><br/>
You will have 15 minutes to complete it. <br /><br/>
You cannot go back to change previous answers.<br /><br/>
Here is your first question: <br /><br />
</p>
<p>1. Abe Lincoln was born in Illinois.</p>
<p>
<label><input type="radio" name="question1" value="true" /> True </label>
<label><input type="radio" name="question1" value="false" /> False </label>
</p>
FIRST;
if (isset($_POST['submit'])) {
$selected_radio = $_POST['question1'];
if ($selected_radio == 'false') {
correct()
}
else {
wrong();
}
}
}
if ($number == 1){
$answer = "Pennsylvania";
$_SESSION["answer"] = $answer;
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
<label><input type="checkbox" name="question2" value="Texas" /> a) Texas </label><br/>
<label><input type="checkbox" name="question2" value="Pennsylvania" /> b) Pennsylvania
</label><br/>
<label><input type="checkbox" name="question2" value="Virginia" /> c) Virginia </label><br/>
<label><input type="checkbox" name="question2" value="West Virginia" /> d) West Virginia
</label>
</p>
SECOND;
if (isset($_POST['submit'])) {
$selected_checkbox = $_POST['question2'];
if ($selected_checkbox = = 'Pennslyvania') {
correct($correct)
}
else {
wrong($answer);
}
}
if ($number == 2){
$answer = "lee";
$_SESSION["answer"] = $answer;
print <<<THIRD
<p>5. The last name of the commander of the Army of North Virginia was __________.</p>
<p>
<input type='text' id='question3' />
THIRD;
if (isset($_POST['submit'])) {
$selected_answer = $_POST['question3'];
if ($selected_answer = = 'lee') {
correct($correct)
}
else {
wrong($answer);
WRONG;
}
}
}
function correct($correct) {
$correct++;
$_SESSION["correct"] = $correct;
return $correct;
print <<<CORRECT
You are correct! Good Job!
<br /><br />
CORRECT;
}
function wrong($answer) {
print <<<WRONG
Sorry, the correct answer is: $answer.
<br /><br />
WRONG;
}
if ($number >= $total_number)
{
print <<<FINAL_SCORE
Your final score is $correct correct out of $total_number. <br /><br />
Thank you for playing. <br /><br />
FINAL_SCORE;
session_destroy();
}
else
{
$number++;
$_SESSION["number"] = $number;
$script = $_SERVER['PHP_SELF'];
print <<<FORM
<form method = "post" action = $script>
$question
<input type = "text" name = "answer" value = "" size = "5" />
<input type = "submit" value = "Check Answer" />
</form>
FORM;
}
print <<<BOTTOM
</body>
</html>
BOTTOM;
?>
No, this isn’t doing it right. Honestly, there’s too much going on here to even really critique. Posting a smaller sample would be easier.
A few things jump out at me:
it’s code like this that people point to when they laugh at PHP. Sorry if that sounds harsh, but it’s true.