Make Your Own PHP Quiz (part 2)

You can download the entire source code for this series here PHP Quiz (11.21 KB)

Part 1 can be seen here

To start building your own php-powered quiz the first thing you need, surprise surprise, is some questions and answers. We want easy access to these later on so we’ll create two arrays, one for the questions and one for the answers.

The $questions Array

This is a straight forward one-dimensional array as follows:

1
$questions = array('Question1','Question2','Question3','etc','etc');

The $answers Array

For the answers, you need to create a two-dimensional array like so:

1
2
3
4
5
$answers = array(
array(0 =>'Question1-CorrectAnswer','Question1-Answer2','Question1-Answer3','Question1-Answer4'),
array(0 => 'Question2-CorrectAnswer','Question2-Answer2','Question2-Answer3','Question2-Answer4'),
array(0 => 'Question3-CorrectAnswer','Question3-Answer2','Question3-Answer3','Question3-Answer4')
);

Two important things to remember here.
First, the order of the arrays inside the $answer array must match the order of the $questions array, that is, the answers to question 1 must be the first array inside $answers and the answers to question 2 must be the second array etc. Secondly, the ‘correct answer’ in each array is explicitly given an index of ‘0′). We’ll use this to reference the correct answer later on.

Once you’ve created your questions and answers, save them in a file called ‘questionsandanswers.php’ or whatever you like. It should look something like this:

questionsandanswers.php

1
2
3
4
5
6
7
8
<?php
$questions = array('Capital City of France','Capital City of Spain','Capital City of England');
$answers = array(
array(0 =>'Paris','Lima','Lisbon','Edinburgh'),
array(0 => 'Madrid','Berlin','Montreal','London'),
array(0 => 'London','Birmingham','Cardiff','Washington')
);
?>

During the quiz, we want a way to randomly order the answers for each question, otherwise the answers would always appear in the same order. While the questions and answers are fresh in our memory, let’s create that shuffling function.

1
2
3
4
5
6
7
8
9
<?php
function shuffle_assoc(&$array) {
    $keys = array_rand($array, count($array));
    foreach($keys as $key)
        $new[$key] = $array[$key];
        $array = $new;
        return true;
}
?>

You don’t need to understand how that function works as it’s a bit beyond the scope of this post. However, understanding what it does is important.
Basically, we pass an array into the function, the function then randomly sorts the array for us and returns the array back to us to use in our script. The crucial benefit of this function is that it retains the association between the array’s keys and values.
What that means is that the ‘correct answer’ will always have the key[0] (which we set earlier) in each array enabling you to access it even after the shuffle. Using the standard shuffle() function would have lost this ‘$key=>$value’ association.
Credit for this function goes to this post on php.net.

Save the code above in a new file ‘functions.php’ and that’s the end of this post.
If you haven’t yet tried the quiz, please do to see the kind of thing you’ll be creating.

Part 1
  • Part 3
  • Part 4
  • Part 5
  • Part 6
  • Final Part
  • Feed IconFollow me on Twitter





    7 Responses to “Make Your Own PHP Quiz (part 2)”

    1. When key[0] all the answers will take the first place ?? how to change aray key for example if the answer is on the second place ?. I have try with aray key 1 and 2 but on the answer page get wrong results..?.

      Can any one tell me how to fix this ?

      Thanks

    2. Hi hasco, all of the correct answers must be first in their respective arrays. That way, you can easily reference them because you know where they are. If you want to have a correct answer anywhere in the array, you’ll have a lot more work to do.
      Sorry if I misunderstood your question :)

    3. Hi ElanMan,

      I have did some programing and this is as follow,

      If “all of the correct answers must be first in their respective arrays” I can always know the right answer just for every question choose the first one and every answer will be good.

      I have try instead of array(0 =>’Paris’,'Lima’,'Lisbon’,'Edinburgh’), to use
      array(‘Lima’ =>’Paris’,'Lima’,'Lisbon’,'Edinburgh’),

      that answer was good but at the end on Result Page i see the changes first the answer is good but the token place is first must be second and the 4 question is not anymore to see can you help me to configure this I’m just like one our busy with your program is beautiful and would like if you u can help me with this correction.

      Thanks

    4. Hey hasco,
      The answers aren’t printed in order for each question.
      Choosing the first answer is not always correct.
      If you look at the code in this page, you’ll see a function called shuffle_assoc(). This shuffles the answers accordingly and prints them to the page but keeps the key => value relationship. So the answers appear in a different order but the ‘correct answer’ always has key[0].
      Hope that makes sense :)

    5. Hi ElanMan,

      Testing on your site i have seen the right way but he he at my test is not really oki he he can you put the new download sample with your code ?

      Thanks,

    6. ElanMan

      I’m not so programmer I just see and change the code I’m sorry if you understand me wrong.

    7. I would like to make something with your program something big thousands of examination but that need more scripting if you want to work with me at this send me n email @ hax-net@hotmail.com

      Regards,

      Hasco

    Leave a Comment







    XHTML: You can use the following tags in your comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">