PHP quiz - help needed.

Discussion in 'Programming' started by Fracisc, Jun 6, 2009.

  1. #1
    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?
     
    Fracisc, Jun 6, 2009 IP
  2. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    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):
     
    risoknop, Jun 6, 2009 IP
  3. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    As Seller:
    100% - 1
    As Buyer:
    50.0% - 1
    #3
    The correct answer could be two options (a and b).
     
    Fracisc, Jun 6, 2009 IP
  4. ankit_frenz

    ankit_frenz Active Member

    Messages:
    1,111
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    63
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    okay the send me the details with the budget you have in mind..i will get it done for you
    thanks
     
    ankit_frenz, Jun 7, 2009 IP
  5. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    As Seller:
    100% - 1
    As Buyer:
    50.0% - 1
    #5
    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.
     
    Fracisc, Jun 7, 2009 IP
  6. Zeest

    Zeest Active Member

    Messages:
    337
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    83
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #6
    Job complete :)
     
    Zeest, Jun 10, 2009 IP