SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick question about creating a basic grading system... please help asap

    So I am building a quick online testing system. I have it setup so I can create multiple choice questions (all questions have exactly 4 answers), save the questions, answers, and the correct answer in mysql db, and have it randomly show 25 questions form the database on the actual testing page. I am stuck on how to actually get the results of what they click on.

    This is the code to show the questions on the test page.

    Code:
    $result8 = mysql_query("SELECT * FROM sampletest ORDER BY RAND() LIMIT 25");
    while($row8 = mysql_fetch_assoc($result8)) {
     
       echo "<table width='200' border='0' cellpadding='0' cellspacing='0'>
          <tr>
            <td><div align='center'>";
    	echo $row8['question'];
    	echo "</div></td>
          </tr>
          <tr>
            <td> <p align='center'>
          <label>
            <select name='";
    	echo $row8['id'];
    	echo "' size='5' id='";
    	echo $row8['id'];
    	echo "'>
              <option value='";
    	echo $row8['a'];
    	echo "'>";
    	echo $row8['a'];
    	echo "</option>
              <option value='";
    	echo $row8['b'];
    	echo "'>";
    	echo $row8['b'];
    	echo "</option>
              <option value='";
    	echo $row8['c'];
    	echo "'>";
    	echo $row8['c'];
    	echo "</option>
              <option value='";
    	echo $row8['d'];
    	echo "'>";
    	echo $row8['d'];
    	echo "</option>
            </select>
            </label>
          <br /><hr>
        </p></td>
          </tr>
        </table>";
    	
    		}
    But I don't understand how I would get the actual grading page to determine whether they have selected the correct answer for each unique question id.

    Any more info I can give and I will.

  2. #2
    SitePoint Zealot
    Join Date
    May 2002
    Location
    RI-USA
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you'd have to create another table in the database with all the correct answers and the corresponding question id. Then on the final page do a lookup and see if they got it right or not.
    Jim

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net/manual/en/faq.htm...aq.html.arrays
    Code:
    <select name="questions[523]">
      <option value="a">...</option>
      <option value="b">...</option>
      <option value="c">...</option>
      <option value="d">...</option>
    </select>
    <select name="questions[199]">
      <option value="a">...</option>
      <option value="b">...</option>
      <option value="c">...</option>
      <option value="d">...</option>
    </select>
    The 523 and 199 would be your unique question ids. I'm assuming they are integers.

    PHP Code:
    // print_r($_POST['questions']);

    $question_ids implode(','array_map('intval'array_keys($_POST['questions'])));

    $sql "
    SELECT question_id
         , answer_code
      FROM questions
     WHERE question_id IN (
    $question_ids)
    "

    PHP Code:
    while ($row mysql_fetch_assoc($res)) {
        if (
    $row['answer_code'] === $_POST[$row['question_id']]) {
            
    //correct
        
    } else {
            
    // wrong
        
    }



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •