Jump to content


Photo

PHP Quiz


  • Please log in to reply
5 replies to this topic

#1 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 23 July 2006 - 05:27 AM

I would like to create a basic quiz. Not the "What kind are you" type. The ones like "You scored 6/10" and such.

Can anyone show me how to create  a basic one?

#2 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 23 July 2006 - 06:10 AM

I'm assuming you know basic PHP, if that be the case, then you should be able to understand and modify this. I'm using 2 questions for an example.

<html>
<head>
<title>PHP Quiz</title>
</head>
<body>
<?php
//Check to see if null values were submitted...
if($_POST['question1'] == "" || $_POST['question2'] == "") {
?>
<form action="quiz.php" method="POST">
<b>Is php a programming language?</b><br>
<select name="question1">
<option value="" selected>Select An Answer</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<b>Ok, now, is C?</b><br>
<select name="question2">
<option value="" selected>Select An Answer</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<input type="submit" value="Submit Questions!">
</form>
<?php
} else {
//Variables, $x records questions correct. $y is the number of questions.
$x = 0;
$y = 2;
//Check the values of the select questions.
if($_POST['question1'] == "yes") {
$x++;
}
if($_POST['question2'] == "yes") {
$x++;
}
?>
<!--Some random HTML can go here...such as styling the page, leave the php inbetween these two comments-->
<?php
echo 'You got '. $x .' out of '. $y .' questions correct.';
?>
<!--Some random HTML can go here...such as styling the page-->
<?php
}
?>
</body>
</html>

Note: This should be stored in a file named quiz.php, unless the form action is changed.

#3 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 23 July 2006 - 07:08 PM

Thanks!

Also, would there be a way so a message appears if you get 2/10 and such?

Like, if you scored 3/10 .. it would say "You need to study more" or something?

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 23 July 2006 - 08:41 PM

Change this:
<?php
echo 'You got '. $x .' out of '. $y .' questions correct.';
?>

To:
<?php
if($x<6){$msg="You need to study more.";}
if($x>5 && $x<10){$msg="Nice, but you could do better.";}
if($x==10){$msg="Perfect!";}
echo 'You got '. $x .' out of '. $y .' questions correct. '.$msg;
?>
 

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 ItsWesYo

ItsWesYo
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 23 July 2006 - 08:58 PM

Thanks again ...

Sorry for these questions ... but I have 1 more. I didn't think of the last two when I was posting this ...

Would there be a way to tell the person which questions they got right or wrong? Example:

Questions they answer:
1) What's my name?
2) What's my age?

Aftermath:
You got 1 out of 2 questions right.

Question 1: RIGHT
Question 2: WRONG



#6 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 24 July 2006 - 10:34 PM

Yes. Here is the script...

<html>
<head>
<title>PHP Quiz</title>
</head>
<body>
<?php
//Check to see if null values were submitted...
if($_POST['question1'] == "" || $_POST['question2'] == "") {
?>
<form action="quiz.php" method="POST">
<b>Is php a programming language?</b><br>
<select name="question1">
<option value="" selected>Select An Answer</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<b>Ok, now, is C?</b><br>
<select name="question2">
<option value="" selected>Select An Answer</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<input type="submit" value="Submit Questions!">
</form>
<?php
} else {
/*Variables, $x records questions correct. $y is the number of questions. $z is ammount of questions wrong.*/
$x = 0;
$y = 2;
$z = 0;
//Check the values of the select questions.
if($_POST['question1'] == "yes") {
$x++;
$q1is = "<br />Question 1: RIGHT";
} else {
$q1is = "<br />Question 1: WRONG";
}
if($_POST['question2'] == "yes") {
$x++;
$q2is = "<br />Question 2: RIGHT";
} else {
$q2is = "<br />Question 2: WRONG";
}
?>
<!--Some random HTML can go here...such as styling the page, leave the php inbetween these two comments-->
<?php
if($x<1){$msg="You need to study more.";}
if($x>0 && $x<2){$msg="Nice, but you could do better.";}
if($x==2){$msg="Perfect!";}
echo 'You got '. $x .' out of '. $y .' questions correct. '.$msg;
echo $q1is;
echo $q2is;
?>
<!--Some random HTML can go here...such as styling the page-->
<?php
}
?>
</body>
</html>

That should output what you are asking.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users