Logo
testing system with arrays
Online Now: 1
Snippet Code
<?php

session_start
(); //start a session in order to be able to use session variables.


//echo "<br>Correct answer from the session: ".$_SESSION['correct_answers'].'<br><br><br>';

// if the get couner value is not set, it's beingset to 0
if(!isset($_GET['counter'])){
    
$_GET['counter'] = 0;
}

// show the respective variables
//echo "The value of GET article:".$_GET['article'].'<br>';
//echo "The value of GET started:".$_GET['started'].'<br>';
//echo "The value of the get counter:".$_GET['counter'].'<br>';
//adding 1 every time the file is loaded
$_SESSION['counter'] +=$_GET['counter'];
//showing the value that we are getting from the line above
//echo "The value of the SESSION counter:".$_SESSION['counter'].'<br>';
// the value of the get answered value, it equals 1 if you answered to a question
//echo "The value of the answered  var: ".$_GET['answered'].'<br>';
$_SESSION['answered'] = $_GET['answered'].'<br>';


//var_dump($_GET['started']);

//var_dump($_SESSION['started']);

//var_dump($_GET['article']);

//var_dump($answers['counter']);

//$_SESSION['counter'] = $_SESSION['counter'] + 1;

//echo $_SESSION['counter'];

// the array that contains all the test sentences
$sentences = array('I have ... apple''... apple is red (the one from the previous sentence)''I also have ... pear.');
//the variable has the amount of total sentences
$questions_totally count($sentences); //count the amount of keys in the sentences array
// showing the amount
//echo "<br>$questions_totally <br>";
//the array that contains the answers
$answers = array('An','The','A');

//showing the values of the answers value in a user friendly way
/*
echo '<pre>';
print_r($answers);
echo '</pre>';

echo "<br>";

*/

?>


<?php



//// echoing to the screen starts right here ////////////////////////////////////////////////////////////////

//it'll show the button, test sentences and answer options as long as the session counter value is less or equals 2; 
//it'll show Thanks otherwise
if($_SESSION['counter'] <= ){    


    


//show the question number
echo "<h3>Question number ".($_SESSION['counter'] + 1)."</h3>";

//if get article coincides with $answers session count valu and started equals 1,  show correct
if($_GET['article'] == $answers[$_SESSION['counter']] and ($_GET['started']  == 1)){
    echo 
" - Correct!<br><br>";  
 
$_SESSION['correct_answers'] = $_SESSION['correct_answers'] + 1
  
    
   
    echo 
"<br>Correct answer from the session: ".$_SESSION['correct_answers'].'<br><br><br>';
// if the articles does not match answers sesson count, show Wrong
} elseif ($_GET['article'] != $answers[$_SESSION['counter']] and $_GET['started']  == 1){
    echo 
" - Wrong!<br><br>";
//otherwise ask to make a choice
} else {
    echo 
" - Please make your choice<br><br><br>";   
}

// showing the percentage
if($_GET['answered']){
$percent = (100/$questions_totally)*($_SESSION['counter'] + 1);
//rounding and showingthe percentage of answered questions
echo '<br>You answered '.round($percent).'% of all the questions<br><br>';
}




// if get answers is empty, show the form with options, it is not set till one clicks the check button
if(empty($_GET['answered'])){
    
// show the test question based on the value of sentences session counter value
echo $sentences[$_SESSION['counter']]."<br>";

?>
<br>
<form action='' method='get'>
<input type='hidden' name='started' value='1'>
<input type='hidden' name='answered' value='1'>
<input type="radio" name="article" value="A"> A<br>
<input type="radio" name="article" value="An"> An<br>
<input type="radio" name="article" value="The"> The<br><br><br>
<input type='submit' value='Check'>
</form>





<?php

}

// show the next button is the answer is available

echo "<br>You answered ".$_SESSION['correct_answers']." questions correctly<br><br><br>";

?>
<form action='' method='get'>
<input type='hidden' name='counter' value='1'>
<input type='submit' value='Next'>
</form>
<?php
} else {
    echo 
"<h3>Thanks</h3>";
}
?>

















 
Snippet Comments

Add Your Comment

No Comments Yet.

Add Your Comment

 
Snippet Tools
Rate this Snippet:

Rate the difficulty level:

Request Snippet Update


Suggested Difficulty Level: Advanced
Current Score: No votes yet

Total Views: 183

Other top snippets by maque:

1. Read file or site in a string
2. If else
3. Types of loops (for, foreach, while)
4. put links on homepage only
5. Showing date

Search
Search for:







User Panel

User name:

Password:

Register And Post Your Own Snippets

New Snippets

1.  Shorten text and (4 of 1)

2.  file-based simple guest (0 of 0)

3.  adding an entry (0 of 0)

4.  integer to currency (1.33 of 3)

5.  Aggregate IP addresses (0 of 0)

6.  file handling (0 of 0)

7.  testing system with (0 of 0)

8.  Make an php (1 of 1)

9.  allow download in (5 of 1)

10.  redirect in 3 (0 of 0)

11.  live clock with (0 of 0)

12.  visit counter (3.5 of 2)

13.  contact form with (4 of 1)

14.  show a specific (0 of 0)

15.  The & sign (0 of 0)

16.  static variable in (0 of 0)

17.  multiplication table with (0 of 0)

18.  function with multiplicationt (0 of 0)

19.  php generated menu (0 of 0)

20.  drawing a table (0 of 0)

21.  embedded array (one (0 of 0)

22.  checks the week (0 of 0)

23.  put links on (4.5 of 2)

24.  Directory Image Gallery (0 of 0)

25.  TOS (4 of 1)

Home | Forum | Free PHP Web Hosting | Contact | Terms & Conditions |  
Donate
PHPSnips.com - ©2012 Amethyst Creative