FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


php msql quiz script





metalfreek
I am trying to write a script that will pull question from the database. Each question will have four options (stored in database) to choose from and the right answer is picked using radio button. After all question has been answered, it will check right answer from database to display the percentage scored.

I would like to start writing the script but I am running out of ideas on how to pull this off. I am kind of stuck in the logic of it. Any suggestions will be appreciated.

Thanks.
jmraker
The database would need to make some tables.
    A quiz table that represents each quiz if you want more than one quiz
    A question table that represents each question
    If you think you'll ever deviate from the 4 choices thing An answer table that represents each answer


The question table would have a foreign key to the primary key of the quiz table, the text of the question. If you didn't create the "answer" table you'd add the fields here and number them 1 to 4.

The answer table would have a foreign key to the question table, the text of the multiple choice answer and a floating point number for how many points the answer is worth. It could be 1 for correct, 0.5 for partial credit and 0 for wrong.

When showing the quiz you'd have to shuffle the choices and/or the questions around with the shuffle function http://php.net/manual/en/function.shuffle.php
The radio boxes in the <form> could contain the keys to lookup the question and answer like
Code:
echo '<label><input type="radio" name="question[' . $questionID . ']" value="' . $valueID . '" />' . $questionText . '</label>';


When scoring the quiz you'd have $_REQUEST['question'] as an array with enough info to get the primary key of the question and which answer they chose to get how many points they got
zimmer
i am not that master on script.. sorry...
metalfreek
jmraker wrote:
The database would need to make some tables.
    A quiz table that represents each quiz if you want more than one quiz
    A question table that represents each question
    If you think you'll ever deviate from the 4 choices thing An answer table that represents each answer


The question table would have a foreign key to the primary key of the quiz table, the text of the question. If you didn't create the "answer" table you'd add the fields here and number them 1 to 4.

The answer table would have a foreign key to the question table, the text of the multiple choice answer and a floating point number for how many points the answer is worth. It could be 1 for correct, 0.5 for partial credit and 0 for wrong.

When showing the quiz you'd have to shuffle the choices and/or the questions around with the shuffle function http://php.net/manual/en/function.shuffle.php
The radio boxes in the <form> could contain the keys to lookup the question and answer like
Code:
echo '<label><input type="radio" name="question[' . $questionID . ']" value="' . $valueID . '" />' . $questionText . '</label>';


When scoring the quiz you'd have $_REQUEST['question'] as an array with enough info to get the primary key of the question and which answer they chose to get how many points they got


Thank you jmraker. Being new to php and mysql makes it a big challenge to generate ideas. Your idea will definitely guide me through this challenge. Now, its time for coding. I will let you know once I finish my coding.
newcomer
Nicely explained... Smile
This'll help me too..
thanks a lot..
LxGoodies
The explanation is very incomplete. Maybe that's why Freek never showed his result here.

In the first post, it is clearly stated that the percentage is reported at the end of the quizz.

Quote:
When scoring the quiz you'd have $_REQUEST['question'] as an array with enough info to get the primary key of the question and which answer they chose to get how many points they got


In any case, there is no ONE total score. It is better to do this in the database, I think.

Keeping the score can be done in arrays, or even in Javascript, Client-side, but if you want to gather the result on the server, it is easier to do it server-side. You will need one additional table for this:

Table: Answers
IPGET CHAR(128)
QuizzID INT
Score FLOAT

For each answer posted by any client, make a new record. Store the ID of the current quizz. See if the answer is correct, store that information in "Score". Also store the IP of the GET-request.

INSERT INTO ANSWERS (..)

At some point, the client wants to know his score. Suppose for each good answer, set the score to 1. Maybe the percentage could be done in one single SQL statement.. I'm not really an expert, but I think the easiest way is to run two queries at the end of a quizz:

1. SELECT SUM(Score) AS TotalScore FROM Answers GROUP BY Answers.QuizzID WHERE (Answers.IPGET = $thisIP) AND (QuizzID = $thisQuizz)

2. SELECT COUNT(*) FROM QuizzQuestions WHERE QuizzQuestion.QuizzID = $thisQuizz

With query 1 you count the score, query 2 will count the number of questions in the current quizz.

You get 2 results in 2 php-variables. The percentage (quizz score) is TotalScore/AnswerCount.

All answers are saved for later use.

Lx
jmraker
I was assuming that all the quiz questions would be asked on one page.

If the questions to the quiz are shown one at a time the user's quiz total could be stored in the session (which has the risk of being up to get garbage collected if the user doesn't answer back in about 20 minutes). Storing it in the database like that you'd need a way to lookup the user, by IP is ok but has the risk of inconveniencing users that don't have a static IP number and it happens to change during the test.
johans
got it..

we have discuss with my client on the same project here is the output of my latest quiz project http://fastsurvey.org/take-a-quiz-challenge - I hope you will find ideas on how it works and response. If you guys are interested on this just email me perhaps i can help.

Have a good day to all!
metalfreek
All right guys. Thanks for your input. Actually I have just completed the script. I had to create a table to store the result of user to check answer later. Here is the site with login and all. Its still in development so you might find it a bit hard to navigate or whatever.

http://www.completeguide.frih.org
LxGoodies
jmraker wrote:
I was assuming that all the quiz questions would be asked on one page.

If the questions to the quiz are shown one at a time the user's quiz total could be stored in the session (which has the risk of being up to get garbage collected if the user doesn't answer back in about 20 minutes). Storing it in the database like that you'd need a way to lookup the user, by IP is ok but has the risk of inconveniencing users that don't have a static IP number and it happens to change during the test.


Yes that's right. I googled for the method, it's even dependent on the server config if you CAN get the client IP. When you're testing locally, if client is using proxie or if the server is VPN (virtual) chance is you will get 127.0.0.0 on any inquiry..

One alternative would be to create a table "QuizzLogins", with an autonumbering index stored in the session for that client. In that case you won't need IP-inquiries. However as you stated already, a session is also temporary.

I think this is the reason why accounts exist. When your application is used seriously, to gather data for research projects.. better create some account management too. Google for it.. you wont need to reinvent the wheel, there are LOTS of php login scripts available online.

Lx
muni
download php script for quiz appliaction here

http://www.smarttutorials.net/responsive-quiz-application-using-php-mysql-jquery-ajax-and-twitter-bootstrap/
TexasCaptain
Nice
amagard
You might be interested in my solution "Quizroom". It is written in Python actually and it generates questions based on facts as I explained here, thus no need to setup prepared questions.
zimmer
Joomla has many choices of quiz application or extension.

try research it.
vat322
Hi, Thats pretty easy. Store the quiz question & four options (with first option as answer) in a database table. While displaying the question, shuffles the options. And When user selects an option check if that value matches with first option in database. If yes, increment his points row or else do nothing.
Related topics
PHP Write to line
script backup database
[php] travel agency script alike
my PHP code benchmark script that got some problems
Quiz script
Help with php and java script
Free PHP/MySQL login script
anyone know of a good php form processer script
A PHP quiz script
Wanting a PHP input security script: up to 1200frih$!
PHP IP tracking script ?
Simple PHP Login Script
PHP - Takasel Membership Script
Php users counter script
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.