I need to create a quiz using PHP that has one question per page that is of the T/F format. The quiz needs to loop back in on itself (so when you get to question 10, you are given question 1 again). Any resources that would help? I'm talking specific resources - not "Look through this website, they might have something there". Thanks!
How do you create a true/false PHP quiz?
Hello, I will try to explain a better way to construct this. I have done the same thing in C#.NET but the theory still is the same for this specific question.
We have to first construct a database to store the quiz questions and the users answers:
* A quiz will have a QUIZ table {"id,name, creationdate"} Where "id", is the id of the quiz such as quiz 1, quiz 2, quiz 3. The name and creationdate are self explanatory.
* A quiz will have QUIZ_QUESTIONS table {"id,quiz_id,name,answer} where the"id" is the current question identifier , and quiz_id is the quiz where that question is refering to that references QUIZ , and name is "what the question is" , and answer would be if the question is 'T' or 'F'
Basically that is a very simple quiz dataapp to add stuff to the database we could do ... To add a new Quiz to database, we do the following:
INSERT INTO quiz VALUES ( 1 , "Bush Trivia", "9/28/2007" )
Now to add Questions to that quiz, you can do the following:
INSERT INTO quiz_questions VALUES ( 1, 1, "Is Bush president of USA", 'Y')
INSERT INTO quiz_questions VALUES ( 2, 1, "Is Bush a girl", 'N')
INSERT INTO quiz_questions VALUES ( 3, 1, "Is Bush dead", 'Y')
So the more questions you add in that table that refers to QUESTION 1 from 2nd column, is the questions for that quiz!
Now that your mini quiz db is setup (note you can extend that table to many tables to make it more general, but for the sake of simplicity, I just did 2 tables which is relevant for this purpose) it is time to do a QUIZ in PHP.
So let us make a file index.php that asks the user to select a quiz from a list, so the user wants to select "Bush Trivia", the user selects the link which is http://www.mysite.com / index.php?quiz=1 Where we introduced a $_GET parameter for the current QUIZ identification number..
NOTE* I am just doing this in a procedural matter, I recommend to do this in an object oriented way
%26lt;?php
if ( isset ( $_GET['quiz'] ) ) {
// Show the QUIIZ
showQuiz($_GET['quiz] );
}
else {
// Print out the list of quiz questions in a link format
}
?%26gt;
Now in ShowQuiz, you have to Gather all the questions, one by one. What you want to do is make the questions hidden from the user so he can't access it next time.. So you must create a SESSION before starting the quiz... and register a sesion that that user has started the quiz.
Now we have a list of all question names in the quiz, I would save that in to a session, so our session manager will contain.
%26lt;?php
session_start();
$_SESSION['quiz'] = the quiz id
$_SESSION['quiz_question'] = the array of quiz questions from the sql query
$_SESSION['quiz_current_id'] = the current id of the question displayed in user so we acan keep track which one is current..
. . .
. . .
?%26gt;
The session should be created once we select on the quiz we decide to do .
in showQuiz( $quiz ) we do a SQL query to the database:
SELECT question.name FROM quiz q JOIN quiz_questiosn qq ON q.id = qq.quiz_id WHERE qq.quiz_id = $quiz
Then we have to manage session variables, so that, you check what the current quiz id and you do But we have to check if we selected NEXT QUESTION button
So when the user selects the NEXT Question button, the POST request will go again, and you have to check if they checked the next button by doing then, you can continue what we said above
%26lt;?php
. . .
. . .
if (isset($_POST['nextButton'] ) {
$_SESSION['quiz_current_id']++;
}
$quizquestion = $_SESSION['quiz_questiosn']
if($_SESSION['quiz_current_id'] %26gt;= arraysize($quizquestion))
{
$_SESSION['quiz_current_id'] = 0; // We go back to the first question
}
ShowQuestionOnScreen($quiz,$_SESSION['...
. . .
. . .
?%26gt;
And in the Show QuestionOnScreen, you can just make a query to the database to grab the contents for that question and print them in the screen. So when the user selects NEXT, it checks if that quiz_queston_id in the session is the last quiz question, and if it is, it will modify it to be the first question so you can manage it. Once you managed that question, you can just print it to the screen.
This is just a BASIC QUIZ app, personally, I wouldn't do it that way. I would use Object Oriented Design to setup the clases, I will use Command Pattern to deal witht he set of Commands so each Command Action is its own Class, to make it easier for me to understand. I will use a more general Database Schema where I will have many tables which will make it larger but more extensible and efficient. I would use a templating systm where I will not mix PHP CODE and HTML, such as Smarty, or just make my own. Basically,I would use the same techniques as I shown above, but I will separate the logic into many classes and make it easier to visualize in a code perspective.
I hope that helped.. Good Luck
Reply:That sort of program sounds pretty easy, assuming, however, that you already know a fair bit of PHP. I highly doubt that there will be any sites saying exactly how to make a script like that, however (even though you said you did not want it) w3school.com has some great tutorials/references. A few ideas to get you started:
-Store the current question Id in a GET variable, so the URL might look like: http://your.site.com/quiz.php?qid=2
-Store the questions, and their answers in an array(s).
-A simple if statement could be used to loop your code. something like:
if($id%26gt;=10) $id=0;
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment