Advertisement
Guest User

Untitled

a guest
Apr 12th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * Assuming data structure:
  5.  *
  6.  * mathbro
  7.  * -------
  8.  * id | question | answer1 | answer2 | answer3 | answer4 | is_right
  9.  * ----------------------------------------------------------------
  10.  *
  11.  */
  12. /**
  13.  * @param PDO     $conn
  14.  * @param integer $question_id
  15.  * @param integer $selected_answer
  16.  *
  17.  * @return bool
  18.  */
  19. function check_answer(PDO $conn, $question_id, $selected_answer) {
  20.     //I'm doing it this way to not complicate things! Normally use prepared statements!
  21.     $query = "SELECT `is_right` FROM `mathbro` WHERE `_id` = $question_id";
  22.  
  23.     //Preform query
  24.     $stmt = $conn->query($query);
  25.     //Fetch results
  26.     $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
  27.  
  28.     //Testing logic
  29.     if ($result["is_right"] == $selected_answer) {
  30.         return true;
  31.     }
  32.     return false;
  33. }
  34.  
  35. /**
  36.  * @param PDO        $conn
  37.  * @param integer    $question_id
  38.  *
  39.  * @return array
  40.  */
  41. function fetch_question(PDO $conn, $question_id) {
  42.     $query  = "SELECT * FROM `mathbro` WHERE `_id` = $question_id";
  43.     $stmt   = $conn->query($query);
  44.     $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
  45.     return $result;
  46. }
  47.  
  48. //DB connection. Using PDO here, better than MySQLi imo.
  49. try {
  50.     $conn = new PDO("mysql:host=localhost;dbname=database", "root", "password");
  51.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Set error reporting to exceptions. Helpful to find errors!
  52. }
  53. catch (PDOException $e) {
  54.     die("Database connection failed! " . $e->getMessage());
  55. }
  56.  
  57. //Check the current question
  58. $current_question = 1;
  59. if (isset($_POST["current_question"])) { //POST is set, question was answered.
  60.     $current_question = $_POST["current_question"];
  61.  
  62.     //Check to see if answer is correct
  63.     try {
  64.         if (check_answer($conn, $current_question, $_POST["ans"])) { //Answer is correct
  65.             $current_question++;
  66.         }
  67.     }
  68.     catch (PDOException $e) {
  69.         die("There was a problem! " . $e->getMessage());
  70.     }
  71. }
  72. try {
  73.     $question = fetch_question($conn, $current_question);
  74.  
  75.     ?>
  76.  
  77. <form action="math.php">
  78.  
  79.     <?php
  80.     if ($current_question == $_POST["current_question"]) { //Variable was not increased, answer isn't correct
  81.         echo "<p class=\"error\">Incorrect! Try again!</p>";
  82.     }
  83.     ?>
  84.  
  85.     <label><input type="radio" name="ans" value="1"><?php echo $question["answer1"]; ?></label>
  86.     <label><input type="radio" name="ans" value="2"><?php echo $question["answer2"]; ?></label>
  87.     <label><input type="radio" name="ans" value="3"><?php echo $question["answer3"]; ?></label>
  88.     <label><input type="radio" name="ans" value="4"><?php echo $question["answer4"]; ?></label>
  89.  
  90.     <input name="current_question" type="hidden" value="<?php echo $current_question; ?>">
  91.  
  92.     <button type="submit">Next</button>
  93. </form>
  94. <?php
  95. }
  96. catch (PDOException $e) {
  97.     die("There was a problem! " . $e->getMessage());
  98. }
  99. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement