User Tools

Site Tools


the_quiz_app_v2

Link to this comparison view

Both sides previous revision Previous revision
Last revision Both sides next revision
the_quiz_app_v2 [2019/08/27 15:07]
nmckillop
the_quiz_app_v2 [2019/08/27 15:52]
nmckillop
Line 5: Line 5:
 This is what your code should look like before we get started, make sure you understand each line (and that it works). ​ Look at the Troubleshooting section below if you run into problems. This is what your code should look like before we get started, make sure you understand each line (and that it works). ​ Look at the Troubleshooting section below if you run into problems.
  
-<​code>​+<​code ​php>
 <?php <?php
 error_reporting(E_ALL);​ error_reporting(E_ALL);​
Line 136: Line 136:
  
 Make sure you have questions in your quiz_questions table (login with Adminer to check) Make sure you have questions in your quiz_questions table (login with Adminer to check)
 +
 +
 +
 +====== Quiz App with Session Management ======
 +<code php>
 +<?php
 +session_start();​
 +error_reporting(E_ALL);​
 +ini_set("​display_errors",​ 1);
 +?>
 +<​h1>​Quiz</​h1>​
 +<?php
 +
 +if(!empty($_REQUEST['​logout'​])) {
 +        session_destroy();​
 +        $_SESSION = null;
 +}
 +
 +if(!empty($_REQUEST['​player_name'​])) {
 +        $_SESSION['​player_name'​] = $_REQUEST['​player_name'​];​
 +}
 +
 +if(empty($_SESSION['​player_name'​])) {
 +        ?>
 +        <​p>​Welcome to the quiz, enter a name to begin:</​p>​
 +        <form method='​post'​ action=''>​
 +        <input type='​text'​ name='​player_name'​ placeholder='​Your name' />
 +        <input type='​submit'​ name='​login_button'​ value='​Start'​ />
 +        </​form>​
 +        <?php
 +        exit();
 +}
 +
 +$player_name = $_SESSION['​player_name'​];​
 +
 +require("​questions_from_db.php"​);​
 +$number_of_questions = sizeof($questions);​
 +
 +if(!empty($_REQUEST['​user_answer'​])) {
 +   ​$user_answer = $_REQUEST['​user_answer'​];​
 +   ​$question_id = $_REQUEST['​random_number'​];​
 +   echo "<​p>​You answered '​$user_answer'​ for Question #​$question_id</​p>";​
 +
 +   ​if($user_answer == $questions[$question_id]['​answer'​]) {
 +        echo "​Correct :)";
 +        $_SESSION[$player_name]['​correct'​][$question_id] = $question_id;​
 +   } else {
 +        echo "Wrong :(";
 +        $_SESSION[$player_name]['​wrong'​][$question_id] = $question_id;​
 +   }
 +
 +}
 +
 +
 +$correct_answers = $wrong_answers = 0;
 +if(!empty($_SESSION[$player_name])) {
 +        if(!empty($_SESSION[$player_name]['​correct'​]))
 +        $correct_answers = sizeof($_SESSION[$player_name]['​correct'​]);​
 +        if(!empty($_SESSION[$player_name]['​wrong'​]))
 +        $wrong_answers = sizeof($_SESSION[$player_name]['​wrong'​]);​
 +        if(!empty($_SESSION[$player_name]['​correct'​])) {
 +                foreach($_SESSION[$player_name]['​correct'​] as $question_id) {
 +                        unset($questions[$question_id]);​
 +                }
 +        }
 +        if(!empty($_SESSION[$player_name]['​wrong'​])) {
 +                foreach($_SESSION[$player_name]['​wrong'​] as $question_id) {
 +                        unset($questions[$question_id]);​
 +                }
 +
 +        }
 +}
 +
 +$total_answered = $correct_answers + $wrong_answers;​
 +
 +echo "<​p>​You'​ve answered $total_answered questions. ​ $correct_answers right, $wrong_answers wrong.</​p>";​
 +
 +
 +$available_questions = $number_of_questions - $total_answered;​
 +?>
 +
 +<​h2>​Welcome <?= htmlentities($_SESSION['​player_name'​]);​ ?></​h2>​
 +<a href='​quiz.php?​logout=1'>​Logout</​a>​
 +<?php
 +
 +if($available_questions > 0) {
 +        $random_question_id = array_rand($questions);​
 +?>
 +
 +<​p><​strong>​Question:​ </​strong><?​= $questions[$random_question_id]['​question'​];​ ?></​p>​
 +<form method='​post'​ action='​quiz.php'>​
 +<input type='​hidden'​ name='​random_number'​ value='<?​= $random_question_id;​ ?>' />
 +<input type='​text'​ name='​user_answer'​ />
 +<input type='​submit'​ name='​answer_button'​ value='​Answer'/>​
 +</​form>​
 +
 +<​p><​a href='​quiz.php'>​Another random question</​a></​p>​
 +<?php
 +} else {
 +?>
 +        <​p>​You'​ve answered all the questions! ​ Your score was <?​=$correct_answers;?>​ out of <?= $number_of_questions;​ ?>​!</​p>​
 +<?php
 +        if(empty($_SESSION[$player_name]['​score_saved'​])) {
 +                $sql = "​insert into high_scores set name=?, correct=?, wrong=?, created_on=now()";​
 +                $insert_stmt = $db_connection->​prepare($sql);​
 +                if($insert_stmt->​execute([$player_name,​ $correct_answers,​ $wrong_answers])) {
 +                        echo "​Highscore saved!";​
 +                } else {
 +                        echo "Error saving highscore";​
 +                        echo print_r($insert_stmt->​errorInfo());​
 +                }
 +        } else {
 +                echo "High score already saved.";​
 +        }
 +
 +}
 +?>
 +
 +
 +
 +</​code>​
the_quiz_app_v2.txt ยท Last modified: 2019/08/27 18:47 by nmckillop