I'm developing a quiz module for a school with php and mysql. The requirements are simple:
1) There are 10 categories(subjects).
2) Select 8 random questions from each category and display them one by one.
3) Get the students input.
4) Calculate the result and display it.
Now, the problem that i face is how to make the script as efficient as possible. Efficient in the sense, make less database hits. I want to store the 80 answers for that particular user in a place and then update them all at once in the DB. I don't want to use cookies. In what way can i achieve that?
Are there any simple php quiz modules that i can refer?
Thanks :)
You clearly want a fast cache layer. I suggest either memcached or APC for that purpose. Session data may also work.
That said, you'd probably be better off using a full-stack PHP framework for this. A good one will manage caches for you. I suggest looking into symfony or CakePHP.
Here's the real question, though: are you trying to address this problem up-front before you have established that there actually is a problem? This is a bad approach. Make sure you have a problem before you try to solve it.
If you have to show them one by one, you need to store the results somewhere and since http is stateless you have 2 choices: db or cookie. Now, in my opinion is better to store the answers in a cookie in a var like this
"a:1,b:2,question:answer,.."
and then explode them at the end and check the result.
You should not want that. It is perfectly normal for a webapp to store intermediate results in a database. Most apps do several database queries per page.
You could use sessions. This allows you to store data which is coupled to a user. However, session data is also stored somewhere, so efficiency is not a reason to store it in a session instead of a database.
From my personal point of view, if I'd be asked to design something similar, I would do the following:
in_array()
on each answer (the array being the correct results) and increment a variable to know how many questions are answered right (that works if every question has the same "value")Enjoy!
What you want to do is a sort of wizard-style flow, with a Next button on each page. This is common to store the results in session for such things.
If you store the questions extracted randomly in session, like this :
$_SESSION['questions'] = array(
array(
'question' => 'What is the response to life ?',
'answer' => '42',
'user_answer' => null, // Will contain the user answer
'answered' => false), // Will be set to true once the question is answered
array(
'question' => 'What is the color of the sky ?',
'answer' => 'blue',
'user_answer' => null,
'answered' => false)
);
Then you could iterate over the questions, keeping trace of the answered questions.