php simple quiz

Can someone point out where in the code i am going wrong?

PHP document

[php]

<?php include("nav2.php");?>
	<div id="quiz_content">
		<img src="img/robin-ban.png" width="300" alt="robin"/>
		<h2>So you think you can be a bartender?</h2>

		<form id="start-quiz" method="post" action="test.php"> 
        	<div class="overlay index">
        		<div class="quiz-overlay"></div>
      			<h1 class="index-headline"></h1>
				<p class="index-sell"></p>              		
            	<a href="test.php">
            		<button type="button" class="btn btn-warning btn-lg">Take a shot</button>
            	</a>
        	</div>                        
    	</form>

	</div>

	<div id="footer">
		<p>&#169; Erin Kelly's Liquor Nerd 2015</p>
	</div>

</body>
[/php] text php doc with questions [php] <?php include("nav2.php");?>
			<div id="quiz_content">
					<form id="quiz" action="grade.php" method="post"> 
						<ol id="exam-questions">
							<li>
								<h3>Question 1</h3>
								<div>
									<input type="radio" name="question-1-answers" id="question-1-answers-a" value="A"/>
									<label for="question-1-answers-a">a) Answer a of question 1</label>
								</div>
								<div>
									<input type="radio" name="question-1-answers" id="question-1-answers-a" value="A"/>
									<label for="question-1-answers-a">b) Answer b of question 1</label>
								</div>						
								<div>
									<input type="radio" name="question-1-answers" id="question-1-answers-a" value="A"/>
									<label for="question-1-answers-a">c) Answer c of question 1</label>
								</div>


							</li>
</ol>
							<input type="submit"value=<button type="button" class="btn btn-warning">Submit</button>/>
					</form>
				</div>
			</div>

		<div id="footer">
		</div>

</body>

grade.php

<?php $answer1 = $_POST['question-1-answers']; $answer2 = $_POST['question-2-answers']; $answer3 = $_POST['question-3-answers']; $answer4 = $_POST['question-4-answers']; $answer5 = $_POST['question-5-answers']; $answer6 = $_POST['question-6-answers']; $answer7 = $_POST['question-7-answers']; $answer8 = $_POST['question-8-answers']; $answer9 = $_POST['question-9-answers']; $answer10 = $_POST['question-10-answers']; $totalCorrect = 0; if ($answer1 == "B") {$totalCorrect+1; } if ($answer2 == "C") {$totalCorrect+1; } if ($answer3 == "A") {$totalCorrect+1; } if ($answer4 == "A") {$totalCorrect+1; } if ($answer5 == "B") {$totalCorrect+1; } if ($answer6 == "C") {$totalCorrect+1; } if ($answer7 == "A") {$totalCorrect+1; } if ($answer8 == "A") {$totalCorrect+1; } if ($answer9 == "A") {$totalCorrect+1; } if ($answer10) {$totalCorrect++; } echo "
$totalCorrect / 10 correct
"; ?>[/php]

Can you point out what’s wrong first? What is the expected outcome, what is the actual outcome?

Part of my degree in computer graphics was game design and I developed a game in Flash that had a little bit of php thrown in. Well a year or two later I decided to convert the trivia game from Flash to php and used that little bit of php to expand it. The little bit used MySQL for storing the questions and answer in a database table, here’s the basic structure of the MySQL:

[php]CREATE TABLE movieTrivia (
id int(11) NOT NULL AUTO_INCREMENT,
confirm enum(‘yes’,‘no’) COLLATE latin1_german2_ci NOT NULL,
question text COLLATE latin1_german2_ci NOT NULL,
answerA char(50) COLLATE latin1_german2_ci NOT NULL,
answerB char(50) COLLATE latin1_german2_ci NOT NULL,
answerC char(50) COLLATE latin1_german2_ci NOT NULL,
answerD char(50) COLLATE latin1_german2_ci NOT NULL,
correct int(1) NOT NULL,
dateAdded timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=63 DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=63 ;[/php]

In a little over 5 years that hasn’t changed and the only thing that has change is mysql is obsolete (I believe it was obsolete back then but at the time I didn’t really know php) and mysqli & PDO (my recommedation) have taken its place. My suggestion is get a basic version of the database table to work strictly in PHP, that is what I did. Then in the near future you could do something like the following:
[php]<?php
require(‘lib/includes/utilities.inc.php’);

unset($_SESSION[‘current_id’]); // Forces user back to first question:
$pageTitle = “Online Movie Trivia”;
include ‘lib/includes/header.inc.php’;
?>

 


Next Question
<?php if ($pageTitle == "Online Movie Trivia") { ?>
    <div class="scoreboard">
        <!-- The Countdown Counter done in jQuery -->
        <div class="stopwatch">
            <p class="countdownClock">Time Left: <span class="timer"></span></p>
        </div>
        <!-- The Score done in jQuery -->
        <div class="scoreBox">
            <p class="score">Points: <span class="totalPoints"></span> </p>
        </div>  
    </div>	
<?php } ?>		

© 2014 J.R. Pepp - All Rights Reserved

[/php]

That’s all there is to the HTML file itself for it also uses Ajax and jQuery(Ajax), though pure php can be used to write the trivia game. I know I have done it strictly in php and if I find it I will post it here.
HTH ~John

Sponsor our Newsletter | Privacy Policy | Terms of Service