Tuesday, April 24, 2012

How to display only one data at a time in an online quiz

I've previously made a text file and turned it into multidimensional array to display as the questions for my quiz.



Note: I am unable to insert images therefore I cannot provide any example so I'll try to be as descriptive as I can.



I'm trying to display only one question at a time, every time a user clicks on my quiz.



This is my code so far. The main.php page:



<h2>ONLINE QUIZ</h2>
<ul>
<li><a href='question.php'>Take quiz</a></li>
<li><a href='module.php'>Admin Module</a></li>
</ul>
<?php

$file = fopen('data.txt', 'r');

$array = array();
while ($line = fgetcsv($file)) {
$array[] = $line;
}

fclose($file);

session_start();
$_SESSION["questions_array"]=$array;

?>


And the question.php page:



<?php

session_start();
$array=$_SESSION["questions_array"];

foreach ($array as $q => $data) {
echo '<p>'.array_shift($data).'</p>';
foreach ($data as $a => $answer) {
echo
' <input type="radio" name="question-'.$q.'" id="question-'.$q.'"'.
' value="'.$a.'"/>'.
' <label for="question-'.$q.'">'.$answer.'</label>'.
'<br>';
}

}

?>


When the Take quiz link is clicked, the user is taken to the question page where only one question is shown. The user then picks an answer and hits submit. This submit button will take the user to the result page where they can then hit continue.



The Continue link will redirect them back to the question page where the next question is displayed.



From stuff that I've done before, I am attempting to use the isset() function to make this happen. However, the problem is that I'm not sure how exactly to write my isset().



I've found this snippet from this site, I'm not sure if it's useful, but:



if (!isset($_SESSION['FirstVisit'])) {

//show site for the first time part
$_SESSION['FirstVisit] = 1;
header("Location: http://example.com/index.php");

// Don't forget to add http colon slash slash www dot before!

} else { Show normal site }


But once again I found myself blank. How exactly do I use the isset() to display only one question?





No comments:

Post a Comment