Point me in the right direction (php/mysql)

Soldato
Joined
25 Jan 2003
Posts
11,550
Location
Newark, Notts
Right I havn't touched php/mysql in about 9 months (and i'd only been doing it for a few months then) so I've pretty much forgot everything. What i'm trying to do is run a test to see how easy it would be to make some sort of quiz.

To begin with I just want to test out a simple question (2+2 efor example). I want each question to be multiple choice and have 4 possible answers for the user to select from. The users answers wil be recorded in order to compare with other users through graphs and what not.

But I'm struggling to get going as I can't get my head round what mysql tables and fields I'd need. Would I need a questions table, and an answers table? And then each answer as a field? :confused: As well as a users table I'm presuming.

Would sessions be used to record users answers?

I'm guessing in the code there'd be something along the lines of "if user selects option1 then overallscore +1" kind of thing, presuming option 1 is the correct answer. But how could I hide that from the user so that they couldn't simply click view source to find the answer?

Anyone point me in the right direction to get me started?
 
You could do it one one table, so long as you know for certain there will be 4 choices. Otherwise, use a questions table, an answers table, and possibly a correct answers table, set up with remote keys.

One table job:
question | ans1 | ans2 | ans3 | ans4 | correctans
secks??? | yes!!! | no!!! | okay! | wtf?? | yes!!!

Not very normalised, but if you desire expansion & flexibility go the multi-table route.

And yeah, use encrypted sessions for their score so they can't easily haxx.
 
Right I havn't touched php/mysql in about 9 months (and i'd only been doing it for a few months then) so I've pretty much forgot everything. What i'm trying to do is run a test to see how easy it would be to make some sort of quiz.

To begin with I just want to test out a simple question (2+2 efor example). I want each question to be multiple choice and have 4 possible answers for the user to select from. The users answers wil be recorded in order to compare with other users through graphs and what not.

But I'm struggling to get going as I can't get my head round what mysql tables and fields I'd need. Would I need a questions table, and an answers table? And then each answer as a field? :confused: As well as a users table I'm presuming.

Would sessions be used to record users answers?

I'm guessing in the code there'd be something along the lines of "if user selects option1 then overallscore +1" kind of thing, presuming option 1 is the correct answer. But how could I hide that from the user so that they couldn't simply click view source to find the answer?

Anyone point me in the right direction to get me started?


Ok you could probably incorporate it into one table for questions and one for users...

Users table...

user_id - int - (auto_incrment)
username - varchar
password - varchar
points - int

Question table...
q_id int (auto_increment)
question varchar
a_a - varchar
a_b - varchar
a_c - varchar
a_d - varchar
answer enum(a,b,c,d)

Display a question > Check the answer and add points

This method above will only give you a score not tell you which ones you got right / wrong at the end. But you could display that information as you go.

if you wanted to know at the end you would...

Users table...

user_id - int - (auto_incrment)
username - varchar
password - varchar

User answers table...

ans_id - int - (auto_increment)
user_id - int
question_id - int
answer - enum (a,b,c,d)

once the person had completed the quiz you would step through all the questions and check for answers compare the correct answer with the user answer and give them a point for each and display the question and right answer along with the user answer. Or some form along those lines.

You could probably have a function like below to check answers in either case.

check_answer($question_id, $answer_id);

it then returns an array with a 1 or 0 for the answer and the question and answers along with the correct answer to display on screen.
 
@unknowndomain - unless the 4 possible answers are always going to be the same, an enum isn't the best thing to use as it's not very flexible, and must be the same for each row. it's also unnecessary to have a user_id column, assuming that your usernames must be unique, as the public key requirements will be filled when you check to see that a username is unique.

tables:
user
username - pk
password
email

questions
questionId - smallint,unsigned,pk, ai
questionText
answerId - ID of the correct answer

answers
answerId - smallint,unsigned,pk, ai
answerText
questionId - ID of the question these answers belong to

attempts
username
questionId
answerId

that will hold all the information you need
 
Last edited:
@unknowndomain - unless the 4 possible answers are always going to be the same, an enum isn't the best thing to use as it's not very flexible, and must be the same for each row. it's also unnecessary to have a user_id column, assuming that your usernames must be unique, as the public key requirements will be filled when you check to see that a username is unique.

No thats not true, your method is better admitedly, however my method doesnt require the same answer each time because in the question table you specify the answers, a. this b. that.... and the correct answer enum = a so when so he checks the user input against the enum and if the answer if = to the enum then yay else nay.

I do prefer your method I just didnt think it through enough.
 
No thats not true, your method is better admitedly, however my method doesnt require the same answer each time because in the question table you specify the answers, a. this b. that.... and the correct answer enum = a so when so he checks the user input against the enum and if the answer if = to the enum then yay else nay.

I do prefer your method I just didnt think it through enough.

riiight, I see what you were doing now! I read it wrong, sorry!
 
Cheers guys, gonna start working on it tomorrow afternoon so i'll probably have more questions then
 
Back
Top Bottom