I want to create a PHP quiz. I have a database where I have added the questions and the answers. Here is the DB structure: CREATE TABLE IF NOT EXISTS `test` ( `id` varchar(10) NOT NULL default '', `question` text NOT NULL, `a1` text NOT NULL, `a2` text NOT NULL, `a3` text NOT NULL, `a4` text NOT NULL, `a5` text NOT NULL, `r1` varchar(10) NOT NULL default '', `r2` varchar(10) NOT NULL default '', `r3` varchar(10) NOT NULL default '', `r4` varchar(10) NOT NULL default '', `r5` varchar(10) NOT NULL default '', UNIQUE KEY `id` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Code (markup): A question can have 5 answer options, people should be able to select one or more options. a1 to a5 are the answer options and r1 to r5 are the correct answers. So, if question one has r3 as correct then r3="correct" and the rest of them are NULL. Now, what I need is a script that will grab 10 randomly selected questions from the DB. I already have that. After this people should make their choices and I need to return the result. This is the harder part for me. I don`t know how to code this part. I should use select boxes on the form but what about the results.php part? How to code that?
First of all, your table is very inpractical, I would use something like: CREATE TABLE quiz ( id INT NOT NULL AUTO_INCREMENT, question VARCHAR(255) NOT NULL, answer1 VARCHAR(255) NOT NULL, answer2 VARCHAR(255) NOT NULL, answer3 VARCHAR(255) NOT NULL, answer4 VARCHAR(255) NOT NULL, answer5 VARCHAR(255) NOT NULL, correct_answer INT NOT NULL, PRIMARY KEY (id) ) ENGINE = INNODB; Code (markup): Secondly, returning the results is really an easy operation. This is a basic form handling. Just grab the values from the $_POST and print them in any way you want. I would just make the page reload and the results would be on on the top of the page. Something like: // load the quiz from the db to the $quiz variable $quiz = ...; // load the submitted answers to the $answers array $answers; $count = null; for ($i = 0; $i < count($answers); $i++) { if ($answers[$i]->answerId == $quiz->corrext_answer) { $count++; } } printf("<p>Result: %s%</p>\n", $count / (count($quiz) / 100)); Code (markup):
I have found a quiz that does 90% of what I need, it only needs a small fix. Anyone available to do that? PM me.