Liquid Digital Lab

PHP Tutorial - Creating a Quiz

Posted 3 years, 3 months ago by John

Simple software applications can be produced very quickly using HTML with the scripting language PHP.  Below is a quiz with five general knowledge questions written using HTML and PHP.

This tutorial assumes some very basic knowledge of HTML and PHP scripting languages and their architectures.

Without going into too much detail, HTML script reads like you do, from top to bottom, left to right. The HTML is separated into certain sections using pre-defined tags. The tags enforce structure. 

An example:-

<strong>Hello World!</strong> The birds are singing!

In the example, the <strong> tag is used to make the Hello World! text bold whereas the, “The birds are singing!” text is of normal weight.  Generally, each tag needs to be started and ended.

The example code below uses a form and table structure to display the questions and answers.  As you can see, the pre-defined tags are used to display the table, <table> and each row, <tr>, and column <td>.  You will notice each tag is ended using the same tag but with a preceding slash. 

The PHP code is used to determine whether the Score button was pressed, and further down calculates the total number of questions that have been answered correctly.

Let us begin…

<!-- These two tags start the HTML code and head sections -->

<html>
<head>

<!-- The title tags displays the page title in the browser -->
<title>The Liquid Quiz</title>
</head>

<!-- The body tags starts the main content of the site -->

<body>

<!-- Display a title in bold -->

<strong>THE Quiz!!!</strong>

<!-- Start the PHP code -->
<?php
// The Score button has not been pressed yet
if ($_POST[”scoreme”] == “”)
{
?>
<!– Start the form and point to the web page that will be loaded when the Score button is pressed –>

<form action='index.php' method='post'>

<!-- Start the table and remove the the border and increase the space between each cell -->

<table border='0' cellpadding='5'>
<tr>
  <!– Display the first question with two possible answers –>

  <td><strong>By what name is the cartoon character, Norville Rogers, better known?</strong></td>
  <td><select name=’questionone’>

  <!-- By giving the correct answer the value of one, the posted data can be simply added up instead of comparing the answers -->

  <option value='0'>Super Ted</option>
  <option value=’1′>Shaggy</option>”
  </select>
  </td>
</tr>
<tr>
  <td><strong>Who is the elder of football’s Charlton brothers? Jack or Bobby?</strong></td>
  <td><select name=’questiontwo’>
  <option value=’0′>Bobby</option>
  <option value=’1′>Jack</option>”
  </select>
  </td>
</tr>
<tr>
  <td><strong>What colour is the letter B in the eBay logo?</strong></td>
  <td><select name=’questionthree’>
  <option value=’0′>Red</option>
  <option value=’1′>Blue</option>”
  </select>
  </td>
</tr>
<tr>
  <td><strong>The Omega watch worn by which actor as he made his debut as James Bond was recently sold at auction for £102,712?</strong></td>
  <td><select name=’questionfour’>
  <option value=’1′>Daniel Craig</option>
  <option value=’0′>Timothy Dalton</option>”
  </select>
  </td>
</tr>
<tr>
<td><strong>What were the Nolan Sisters in the mood for in 1979?</strong></td>
  <td><select name=’questionfive’>
  <option value=’1′>Dancing</option>
  <option value=’0′>Drinking</option>”
  </select>
  </td>
</tr>
<tr>
  <!– Display the Score button.  Once clicked, the page will be reloaded with the answers given.  This section of code will then be skipped. –>

  <td colspan='2'><input type="submit" value="Score" name="scoreme" />
  </td>
</tr>
</table>
<?php
}

// Questions have been answered and the Score button has been pressed
else
{
// Set up the total, giving it an initial value of 0
$total = 0;

// Calculate the total score from each answer
$total = $_POST[”questionone”] + $_POST[”questiontwo”] + $_POST[”questionthree”] + $_POST[”questionfour”] + $_POST[”questionfive”];

// Display two blank lines and then display the total out of 5.
echo “<br /><br />You got “.$total.” out of 5″;
}

// End the PHP code
?>
<!– End the tags, started at the top of the code –>
</body>
</html>

Leave a Comment

Three D Clock

Classic clock built in processing

Posted by Lloyd - Comments (0)

This Happened #5

Lloyd gets chance to go along to the 5th gathering of This Happened

Posted by Lloyd - Comments (0)

Tom survives in one piece!

Tom makes it back to the office through 19 peaks over 149 miles, 60 flood warnings, 60mph gusts and 48hours of torrential rain, all in the name of MS Society, Wheels4Life and a weekend to remember!

Posted by Tom - 3 Comments (3)

Liquid goes Coast to Coast

Inspired by his managers previous fundraising wetsuit exploits, our newest designer Tom will be tackling the coast to coast bike ride this September.

Posted by Tom - Comments (1)

Liquid Artwork

Have a look at the artwork we created for the front office

Posted by Lloyd - 3 Comments (3)

Decisions, decisions… A Bit More Game AI

Game AI goes a few steps forwards, and 2000 years into the past…

Posted by Simon - 3 Comments (3)

Ever Won The Lottery?

Winning The Big One!
I recently read somewhere that winning the UK national lottery is easy. All you have to do is …

Posted by Simon - Comments (1)

3DS Max - Basics and Blueprints

The start of a series of tutorials in 3ds max from Liquids latest addition to the creative team.

Posted by Lloyd - 9 Comments (9)

Sound Reactive Sketch

We created this experimental Music Visualiser in processing & minim sound library.

Posted by Lloyd - 2 Comments (2)

Pathfinding in Games - Using A* in Director

Discover the joys of the A* pathfinding algorithm in our retro Director maze

Posted by Simon - Comments (1)

Colour Clock Screensaver

We’ve made an animated clock screensaver. You can have it too.

Posted by Ben - 2 Comments (2)

Using the Flash Tween Class

Trying to create realistic bouncing balls or elastic objects in Flash? Ben Stevens shows us the easy way to code motion using the Flash Tween Class.

Posted by Ben - 6 Comments (6)

A Jungle Jumble Using Lists in Director

What do you get if you cross a monkey, an elephant and a giant chicken-thing? Simon Edwards tries to explain using lists in Director.

Posted by Simon - 3 Comments (3)

Lloyd Survives the 2007 London Triathlon

Liquid’s very own budding triathlete successfully completes the Michelob ULTRA London Triathlon in a time of 2 hours and 42 minutes. Find out how he got on.

Posted by Lloyd - Comments (1)

Simple AI in Director

Simon Edwards discusses some of the techniques that developers call upon when programming games, particularly for defining logic that the computer will use for taking a turn in a 2-player game scenario.

Posted by Simon - Comments (0)

Ruby on Rails Tutorial - An Online DVD Catalogue

John Cove takes us through the process of making an online catalogue using RoR.

Posted by John - Comments (0)

Why write an ActiveX control?

Howard Peters discusses the advantages of using ActiveX, and explains how to create your own simple ActiveX control.

Posted by Howard - Comments (0)

Nichola’s Placement Year

Nichola, our resident student placement, has now been with us for a whole year and will be leaving us soon. So we asked her what it’s been like to work with the Liquid team.

Posted by Nichola - Comments (0)

Kinetica Soundwaves Exhibition

Exhibition visit

Posted by Ben - Comments (0)

PHP Tutorial - Creating a Quiz

John Cove explains how to use HTML and PHP to create an online quiz.

Posted by John - Comments (0)

Director Tutorial - Vector Font Outlines

Simon Edwards demonstrates an undocumented Director feature enabling you to map out a string of text in a vector cast member.

Posted by Simon - Comments (0)

HDR Photography

HDR (High Dynamic Range), is a software based, photographic technique that extracts tonal information from multiple exposures of the same shot. If you exposure bracket the same shot, you can merge all 3 images into one, then, using the data from all 3, create one output file which preserves accurate details from all exposures, bringing out highlights from the shadows.

Posted by Darren - Comments (0)

Happy Birthday To Us!

Liquid Digital Ltd has reached the end of its first financial year. There’s no time for cake and a candle though - there are some big things on the horizon for the first quarter of Year 2. Watch this space for news soon.

Posted by Simon - Comments (0)

Director Tutorial - Counting Down To A Launch Date

Tutorial written by our resident Director Guru, Simon Edwards.

Posted by Simon - Comments (0)

Lloyd in a wetsuit

Our creative leader will be braving the elements by competing in the Michelbob ULTRA London Triathlon on 4th August 2007.

Posted by Ben - Comments (0)