This web based quiz uses javascript, php and mysql to:
- Capture the users answers
- Compare them against the correct answers
- Give the user a score
- If the score is one of the top 5 it will add it to the leader board

The questions are displayed and users answers captured in the index.php file, when the user is finished
or the time limit (90 seconds) has passed, the selection is then submitted using a form to the answers.php
file where the users answers and correct answers are compared, the user is given 2 points for every correct
answer and has one dedicated for every wrong answer.
The users score is then compared against the 5th highest score in the mysql database, if it is larger the user
is given the chance to enter their name, if it isn’t they are given the option to retake the quiz.
If they have one of the top 5 scores, the users name along with their score is submitted to the database using
insert.php and the leader board is finally displayed in scores.php.
connect.php simply stores the code to connect to the database, it is included multiple times.

The mysql database has only 3 columns:
- id (auto incrementing int, primary key)
- name (char)
- score (int)

Which are all pretty self explanatory.

Any questions please contact me.

Better write up in progress.

<?php

        //Initialise connection to the database.
        $dbhost = 'database hostname';
        $dbuser = 'database user';
        $dbpass = 'database password';

        $conn = mysql_connect($dbhost, $dbuser, $dbpass)
        or die ('Error: Cannot connect to database');

        $dbname = 'database name';
        mysql_select_db($dbname);


?>

<html>

<head>

  <title>Quiz</title>

    <!-- 90 second timer to complete quiz, the 90000 is the time in milliseconds -->
      <script type="text/JavaScript">
        setTimeout("quiz.submit()",90000);
    </script>
</head>

<body>

    <table>
    
        
        <!-- Quiz, form using select boxs which sends the users choices
        (as 1, 2 or 3 for each question) to answers.php
        using the POST method when the submit button is pressed -->
        
        <form action="answers.php" method="post" name="quiz">

        <tr>
                <td> You Have 90 Seconds to Complete the Quiz! </td>
      
          </tr>


            <tr>
                <td> Question 1: Which frequency does WiFi 802.11 B/G operate on? </td>
                <td>
                    <select name="Q1">
                      <option value="1"> 2.1GHz </option>
                      <option value="2"> 2.4Ghz </option>
                      <option value="3"> 3.2Ghz </option>
                  </select>
              </td>
      
          </tr>
          <tr>
      
              <td> Question 2: Which type of display does not need independent backlighting? </td>
              <td>
                    <select name="Q2">
                        <option value="1"> LCD </option>
                        <option value="2"> Plasma </option>
                        <option value="3"> OLED </option>
                    </select>
                </td>
            </tr>
            
            
            <tr>
                <td> Question 3: The European GNSS system designed to supersede GPS is called  </td>
                <td>
                    <select name="Q3">
                      <option value="1"> Galileo </option>
                      <option value="2"> Leonardo </option>
                      <option value="3"> Columbus </option>
                  </select>
              </td>
          </tr>
          
          
            <tr>
                <td> Question 4: Which classic game did Apple Co-Founder Steve Wozniak work on? </td>
                <td>
                    <select name="Q4">
                      <option value="1"> Pong </option>
                      <option value="2"> Space Invaders </option>
                      <option value="3"> Breakout </option>
                  </select>
              </td>
            </tr>
            
            
            <tr>
                <td> Question 5: What year where Compact Disks (CD's) Introduced? </td>
                <td>
                    <select name="Q5">
                      <option value="1"> 1982 </option>
                      <option value="2"> 1985 </option>
                      <option value="3"> 1989 </option>
                  </select>
              </td>
            </tr>
            
            
            <tr>
                <td> Question 6: Before transistors, what where used as electronic switches in computers? </td>
                <td>
                    <select name="Q6">
                      <option value="1"> Bulbs </option>
                      <option value="2"> Valves </option>
                      <option value="3"> Kittens </option>
                  </select>
              </td>
            </tr>
            
            
            <tr>
                <td> Question 7: A high definition image consists of a minimum of how many progressive lines? </td>
                <td>
                    <select name="Q7">
                      <option value="1"> 480 </option>
                      <option value="2"> 590 </option>
                      <option value="3"> 720 </option>
                  </select>
              </td>
            </tr>
            
            
            <tr>
                <td> Question 8: Which of these is not a widely used technology in touch displays? </td>
                <td>
                    <select name="Q8">
                      <option value="1"> Resistive </option>
                      <option value="2"> Reflective </option>
                      <option value="3"> Capacitive </option>
                  </select>
              </td>
            </tr>

            
            <tr>
                <td> Question 9: In which year was the web first conceived? </td>
                <td>
                    <select name="Q9">
                      <option value="1"> 1989 </option>
                      <option value="2"> 1990 </option>
                      <option value="3"> 1991 </option>
                  </select>
              </td>
            </tr>

            
            <tr>
                <td> Question 10: How much data did the first iPod store? </td>
                <td>
                    <select name="Q10">
                      <option value="1"> 4GB </option>
                      <option value="2"> 5GB </option>
                      <option value="3"> 8GB </option>
                  </select>
              </td>
            </tr>


        
        
    </table>
    
      <br/> <br/>
      <input type="submit" />

    </form>
    
    


</body>

</html>

<html>

    <head>

    </head>

<body>

    <?php    
    
        include("connect.php");
        
        //Declares a variable in which the users current score is held
        $score = 0 ;
        
        /*
        The folling php code is made up of several statements and loops which
        1) Print the question number.
        2) uses an if statement to see if the users answer was correct
        if so it tells the user they where correct and adds 2 onto the variable score.
        if the answer was not correct the else part of the statement kicks in
        and tells the user they where incorrect and minuses 1 from their score.
        3) A break is printed to seperate the next answer.
        This is then repeated for each question.
        */
        
        //Question 1
        echo "Q1: " ;
        
        if ($_POST["Q1"] == "2")
        {
        echo "Correct: WiFi B+G operate on the 2.4GHz frequency, along with Microwave ovens." ;
        $score = $score + 2;
        }
    
        else
        {
        echo "Incorrect: WiFi B+G operates on the 2.4GHz frequency, along with Microwave ovens." ;
        $score = $score - 1;
        }
        
        
        echo "<br/>" ;
        
        
        //Question 2
        echo "Q2: " ;
        
        if ($_POST["Q2"] == "3")
        {
        echo "Correct: It is in fact diodes in OLED displays which produce their own light." ;
        $score = $score + 2;
        }

        else
        {
        echo "Incorrect: It is in fact diodes in OLED displays which produce their own light." ;
        $score = $score - 1;
        }
        
        
        echo "<br/>" ;
        
        
        //Question 3
        echo "Q3: " ;
        
        if ($_POST["Q3"] == "1")
        {
        echo "Correct: Galileo, due to launch in 2013 uses 30 satellites and will be more accurate than GPS." ;
        $score = $score + 2;
        }

        else
        {
        echo "Incorrect: Galileo, due to launch in 2013 uses 30 satellites and will be more accurate than GPS." ;
        $score = $score - 1;
        }
        
        
        echo "<br/>" ;
        
        
        //Question 4
        echo "Q4: " ;
        
        if ($_POST["Q4"] == "3")
        {
        echo "Correct: Steve Wozniak help create the hardware for the original Breakout game while working at Atari." ;
        $score = $score + 2;
        }

        else
        {
        echo "Incorrect: Steve Wozniak help create the hardware for the original Breakout game while working at Atari." ;
        $score = $score - 1;
        }
        
        
        echo "<br/>" ;
        
        
        //Question 5
        echo "Q5: " ;
        
        if ($_POST["Q5"] == "1")
        {
        echo "Correct: CDs first appeared on the market in October of 1982." ;
        $score = $score + 2;
        }

        else
        {
        echo "Incorrect: CDs first appeared on the market in October of 1982." ;
        $score = $score - 1;
        }

        
        echo "<br/>" ;
        
        
        //Question 6
        echo "Q6: " ;
        
        if ($_POST["Q6"] == "2")
        {
        echo "Correct: Valves where used as electronic switched before the introduction of transistors." ;
        $score = $score + 2;
        }

        else
        {
        echo "Incorrect: Valves where used as electronic switched before the introduction of transistors." ;
        $score = $score - 1;
        }
        
        
        echo "<br/>" ;
        
        
        //Question 7
        echo "Q7: " ;
        
        if ($_POST["Q7"] == "3")
        {
        echo "Correct: High definition images are made up of a minimum of 720 progressively rendered lines." ;
        $score = $score + 2;
        }

        else
        {
        echo "Incorrect: High definition images are made up of a minimum of 720 progressively rendered lines." ;
        $score = $score - 1;
        }
        
        
        echo "<br/>" ;
        
        
        //Question 8
        echo "Q8: " ;
        
        if ($_POST["Q8"] == "2")
        {
        echo "Correct: Resistive and Capacitive touch displays are widely used today." ;
        $score = $score + 2;
        }

        else
        {
        echo "Incorrect: It is in fact Resistive and Capacitive touch displays that are widely used today." ;
        $score = $score - 1;
        }


        echo "<br/>" ;
        
        
        //Question 9
        echo "Q9: " ;
        
        if ($_POST["Q9"] == "1")
        {
        echo "Correct: The web was first conceived by Tim Berners-Lee while working at CERN in 1989." ;
        $score = $score + 2;
        }

        else
        {
        echo "Incorrect: The web was first conceived by Tim Berners-Lee while working at CERN in 1989." ;
        $score = $score - 1;
        }
        
        
        echo "<br/>" ;
        
        
        //Question 10
        echo "Q10: " ;
        
        if ($_POST["Q10"] == "2")
        {
        echo "Correct: The first iPod held 5GB allowing for a total of around 1000 songs." ;
        $score = $score + 2;
        }

        else
        {
        echo "Incorrect: The first iPod held 5GB allowing for a total of around 1000 songs." ;
        $score = $score - 1;
        }
        
        
        echo "<br/> <br/>" ;
        
        //Prints out the users score (stored in the variable score).
        echo "Your Score: " . $score ;
        
        /*
        The following starts a php session which allows the insert.php script
        access to the users total score.
        */
        session_start();
        
        //users score saved as a variable "total_score" in the php session.
        $_SESSION['total_score']="$score" ;
        

        if($score < 5)
        {
        $back = "#FF7F50";
        }
        
        else
        {
        $back = "#AFEEEE";
        }
        
        // Checks if user's score has made leaderboard
        // Select table and columns upto 5th result
        $leader = mysql_query("SELECT score
        FROM TESTTABLE
        ORDER BY score DESC
        LIMIT 5");
        
        // Select the 5th row (bottom of leaderboard)
        while($row = mysql_fetch_array($leader))
        {
        $bottomlead = $row['score'] ;
        }
        
        // Compare bottom of leaderboard to users score
        /*
        if score is large enough for leaderboard
        users name inputted to a text input inside a form,
        which is then posted to the insert.php script file.
         */
        if($score > $bottomlead)
        {
        echo "<br />" ;
        echo "Congratulations! Your score made it to the Leader board.";
        echo "<br /> Please enter your name to submit your score:";
        echo '<form action="insert.php" method="post">
        <input type="text" name="name" />
        <input type="submit" />
        </form>' ;
        }
        
        // else invite the user to take the quiz again
        else
        {
        echo "<br /> <br />" ;
        echo "Sorry! Your score didn't make the Leader board.";
        echo '<br /> <a href="quiz.php">Try Again</a>';
        }
    ?>
    
    <!-- change background colour depending on score -->
    <script language="javascript">
    
        document.body.style.backgroundColor="<?php echo $back; ?>";
    
    </script>


</body>

</html>

<?php

        /*
        This script file is used to save the users name & score
        to a mysql database.
        */

        include("connect.php");
        
        session_start();
        
        
        //Insert the users name (posted from answers.php) and score stored in the session as "total_score".
        mysql_query("INSERT INTO TESTTABLE (name, score)
        VALUES ('$_POST[name]', '$_SESSION[total_score]')");
        
        //close the connection to the database.
        mysql_close($con)
        
?>

<!-- Forward the user to the leaderboard page -->
<script>
<!--
  window.location= "scores.php"
//-->
</script>

    <html>

        <head>

            <title> Leader Board </title>

        </head>

        <body>

            <h1> Leader Board </h1>

            <br/>

            <?php

            //The following displays the top 5 scores stored (decending) from the database in a table.

            include("connect.php");

            //Query database for users name and score ordered decending and limit to the 5 top users.
            $result = mysql_query("SELECT name, score
            FROM TESTTABLE
            ORDER BY score DESC
            LIMIT 5");

            //Creates a table and places a headder row inside it
            echo "<table border='1' width='500' ALIGN='center'>";
            echo "<tr> <th> # </th> <th> Name </th> <th> Score </th> </tr>";

            //Declares a count variable used to print the users position in the leaderboard.
            $count = "1";

            //Loop to print the leaderboard, while data is being fed from the query above execute the loop.
            while($row = mysql_fetch_array($result))
                  {
                  //start a new row
                  echo "<tr>";
                  //create a new cell with the value of the count variable inside it
                  echo "<td>" . $count . "</td>";
                  //create a new cell with the users name stored in the current row of the database.
                  echo "<td>" . $row['name'] . "</td>";
                  //create a new cell with the users score stored in the current row of the database.
                  echo "<td>" . $row['score'] . "</td>";
                  //end the current row
                  echo "</tr>";
                  //incriment the variable count by 1
                  $count++;
                  }

            echo "</table>";


            //close the connection to the database.
            mysql_close($conn);
            ?>

            <br/>
            <a href="quiz.php">Try Again</a>

        </body>

    </html>