ItsWesYo Posted July 23, 2006 Share Posted July 23, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/15376-php-quiz/ Share on other sites More sharing options...
AncientSage Posted July 23, 2006 Share Posted July 23, 2006 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.[code]<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--><?phpecho 'You got '. $x .' out of '. $y .' questions correct.';?><!--Some random HTML can go here...such as styling the page--><?php}?></body></html>[/code]Note: This should be stored in a file named quiz.php, unless the form action is changed. Quote Link to comment https://forums.phpfreaks.com/topic/15376-php-quiz/#findComment-62332 Share on other sites More sharing options...
ItsWesYo Posted July 23, 2006 Author Share Posted July 23, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/15376-php-quiz/#findComment-62472 Share on other sites More sharing options...
Orio Posted July 23, 2006 Share Posted July 23, 2006 Change this:[code=php:0]<?phpecho 'You got '. $x .' out of '. $y .' questions correct.';?>[/code]To:[code=php:0]<?phpif($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;?>[/code][hr]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/15376-php-quiz/#findComment-62496 Share on other sites More sharing options...
ItsWesYo Posted July 23, 2006 Author Share Posted July 23, 2006 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: RIGHTQuestion 2: WRONG Quote Link to comment https://forums.phpfreaks.com/topic/15376-php-quiz/#findComment-62503 Share on other sites More sharing options...
AncientSage Posted July 24, 2006 Share Posted July 24, 2006 Yes. Here is the script...[code]<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--><?phpif($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>[/code]That should output what you are asking. Quote Link to comment https://forums.phpfreaks.com/topic/15376-php-quiz/#findComment-63160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.