I just started learning how to code PHP as well as HTML and as a challenge for myself I put together a questionnaire with what I have learnt so far which isn't as much as I would like to know e.g. sessions, databases, etc. Anyhoo, I would like to get the review of peers and would appreciate any constructive suggestions, ideas, etc. Here it does...
//Initialize page
$startpage = isset($_POST['page']) ? $_POST['page'] : 1;
$endpage = 11;
//Initialize score
$score = isset($_POST['score']) ? $_POST['score'] : 0;
if(isset($_POST['submit'])) {
$questions = array('question1' => 'superglobal', 'question2' => 'formsubmission', 'question3' => 'declare', 'question4' => 'symbol', 'question5' => 'statement', 'question6' => 'division', 'question7' => 'codeans',
'question8' => 'email', 'question9' => 'super', 'question10' => 'switch');
foreach($questions as $questionkey => $questionvalue) {
$questionpost = isset($_POST[$questionvalue]) ? $_POST[$questionvalue] : '';
if($questionkey == 'question'.$startpage && empty($questionpost)) {
$errormsg = 'Please select an answer for '. $questionkey;
echo $errormsg . '<br />';
}
}
//Answers
$answers = array('question1' => 'if', 'question2' => 'post', 'question3' => '$a', 'question4' => ';', 'question5' => '3', 'question6' => '%', 'question7' => 'infinite', 'question8' => 'mail',
'question9' => array('get', 'post', 'request'), 'question10' => array('break'));
//Loop through post array
foreach($questions as $questionkey => $questionvalue) {
// echo $postkey. '=' .$postvalue;
if(!empty($_POST[$questionvalue]) && $questionkey == 'question'.$startpage) {
//Loop through answers
foreach($answers as $key => $value) {
if(is_array($value)) {
foreach($value as $arraykey => $arrayvalue) {
if($questionkey == $key && $_POST[$questionvalue][$arraykey] == $arrayvalue) {
// echo $key. '=' .$arrayvalue;
echo 'Your answer for '. $questionkey .' was correct';
$startpage = $startpage + 1;
$score = $score + 1;
break;
}
else if($questionkey == $key && $_POST[$questionvalue][$arraykey] != $arrayvalue) {
echo 'Your answer for '. $questionkey .' was incorrect';
echo $_POST[$questionvalue][$arraykey];
break;
}
}
}
else {
if($questionkey == $key && $_POST[$questionvalue] == $value) {
// echo $key. '=' .$value. '<br />';
echo 'Your answer for '. $questionkey .' was correct';
$startpage = $startpage + 1;
$score = $score + 1;
// echo $questionkey;
// echo $key;
break;
}
else if($questionkey == $key && $_POST[$questionvalue] != $value) {
echo 'Your answer for '. $questionkey .' is incorrect';
// echo $questionvalue;
// echo $questionkey;
// echo $key;
break;
}
}
}
}
}
//Debug
// print_r($_POST);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Questionnaire</title>
</head>
<body>
<h1>Questionnaire</h1>
<p>This is a sample questionnaire that you can take and evaluate your results. Below are some tips</p>
<ol>
<li>This questionnaire has total of 10 questions</li>
<li>Each question is worth 1 point totalling 10 points</li>
<li>There are 8 multiple choice and 2 multi-select questions</li>
<li>At the end of the questionnaire, you'll be presented with your score</li>
</ol>
<form name="questionnaire" method="post" action="questionnaire.php">
<?php
echo
'<input type="hidden" name="page" value="'. $startpage. '">';
echo
'<input type="hidden" name="score" value="' . $score .'">';
switch($startpage) {
case(1):
echo
'<div> Question 1. Which one of these is a super global? <br />
<input type="radio" name="superglobal" value="if">if
<input type="radio" name="superglobal" value="and">and
<input type="radio" name="superglobal" value="get">GET
<input type="radio" name="superglobal" value="array">array
</div>';
break;
case(2):
echo
'<div> Question 2. Which one of these hides the values when submitting a form? <br />
<input type="radio" name="formsubmission" value="get">GET
<input type="radio" name="formsubmission" value="send">SEND
<input type="radio" name="formsubmission" value="parcel">PARCEL
<input type="radio" name="formsubmission" value="post">POST
</div>';
break;
case(3):
echo
'<div> Question 3. Select which one these is the correct way of declaring a variable? <br />
<input type="radio" name="declare" value="a">a
<input type="radio" name="declare" value="$a">$a
<input type="radio" name="declare" value="%a">%a
<input type="radio" name="declare" value="%a">%a
</div>';
break;
case(4):
echo
'<div> Question 4. Which symbol would you have at the end of a statemement <br />
<input type="radio" name="symbol" value=";">;
<input type="radio" name="symbol" value="#">#
<input type="radio" name="symbol" value="*">*
<input type="radio" name="symbol" value="/">/
</div>';
break;
case(5):
echo
'<div> Question 5. Select which one of these statements is true? <br />
<input type="radio" name="statement" value="2">a++ equals to 2 if a is 1
<input type="radio" name="statement" value="3">++a is equals to 3 if a is 2
<input type="radio" name="statement" value="divide">\ is the symbol for divisions
<input type="radio" name="statement" value="zero">0 is the value that represents true
</div>';
break;
case(6):
echo
'<div> Question 6. Which symbol gives the remainder in a division? <br />
<input type="radio" name="division" value=")">)
<input type="radio" name="division" value="}">}
<input type="radio" name="division" value="%">%
<input type="radio" name="division" value=";")>;
</div>';
break;
case(7):
echo
'<div> Question 7. What will the code below return <br />
<pre>
$a=1
while($a < 10) {
echo $a;
}
</pre>
<input type="radio" name="codeans" value="one">1
<input type="radio" name="codeans" value="zero">0
<input type="radio" name="codeans" value="infinite">infinite loop
<input type="radio" name="codeans" value="3">3
</div>';
break;
case(8):
echo
'<div> Question 8. What function can you use to send an email <br />
<input type="radio" name="email" value="mail">mail
<input type="radio" name="email" value="email">email
<input type="radio" name="email" value="mailto">mailto
<input type="radio" name="email" value="send">send
</div>';
break;
case(9):
echo
'<div> Question 9. Which of the following are super-globals?<br />
<input type="checkbox" name="super[]" value="get">GET
<input type="checkbox" name="super[]" value="post">POST
<input type="checkbox" name="super[]" value="request">REQUEST
<input type="checkbox" name="super[]" value="process">PROCESS
</div>';
break;
case(10):
echo
'<div> Question 10. Which of the following are true to exit a switch statement? <br />
<input type="checkbox" name="switch[]" value="exit">exit
<input type="checkbox" name="switch[]" value="die">die
<input type="checkbox" name="switch[]" value="drop">drop
<input type="checkbox" name="switch[]" value="break">break
</div>';
break;
case(11):
echo 'Congratulations, you have completed the test and you scored.'. $score;
break;
}
?>
<?php
if($startpage < $endpage) {
echo '<div><input type="submit" name="submit" value="submit"></div>';
}
?>
</form>
</body>
</html>
2 Answers 2
I think you may improve your code by using MVC pattern. In your case, for example, I had recommend:
- extract all logic to class (don't print any message or use $_POST directly from it).
- try to minimize mixing HTML and PHP code
- try to minimize any logic in View (for example, instead of
if ($startpage < $endpage)
I preferif ($showSubmitButton)
) - use session to store score
- switch with all questions looks monstrous, so IMHO you may move each question to file like
1.txt
and use something likereadfile($startpage)
(don't forget to check it for integer and allowed range before!)
-
\$\begingroup\$ Thanks. I am new to the world of coding and have no idea how to implement MVC however I will certainly look into how to use MVC. In a layperson's example, what is MVC, how does it work, what do I have to do different? When you say 'extract all logic to class (don't print any message or use $_POST directly from it).', what do you mean exactly? Can you give me examples? In the your example of using if ($showSubmitButton)) would $showSubmitbutton = $startpage < $endpage? \$\endgroup\$PeanutsMonkey– PeanutsMonkey2011年04月20日 21:13:43 +00:00Commented Apr 20, 2011 at 21:13
-
\$\begingroup\$ continued; I agree that the swtich statements are monstorus but hope to improve the code once I learn how to read from a file and database. As for using sessions, I have yet to learn how to use them. Overall, how would you rate my coding for a person who just started learning how to code? \$\endgroup\$PeanutsMonkey– PeanutsMonkey2011年04月20日 21:14:01 +00:00Commented Apr 20, 2011 at 21:14
-
\$\begingroup\$ @PeanutsMonkey MVC stands for Model-View-Controller, and it's what's called a design pattern. Design patterns are, well... Software designs that follow certain patterns or structures of proven usability. In an MVC patterned application, your data, logic and output are all abstracted to different processes relatively independent of each other. You'll learn more about design patterns when you study object-oriented programming. \$\endgroup\$65Fbef05– 65Fbef052011年04月21日 01:40:34 +00:00Commented Apr 21, 2011 at 1:40
-
\$\begingroup\$ Your code is better than many newbie writes, so go ahead! And yes,
$showSubmitButton
should be initialized by$startpage < $endpage
. \$\endgroup\$php-coder– php-coder2011年04月21日 03:34:39 +00:00Commented Apr 21, 2011 at 3:34 -
\$\begingroup\$ && 65Fbef05 - Thanks for the heads-up. I'll definitely look at the Model-View-Controller. \$\endgroup\$PeanutsMonkey– PeanutsMonkey2011年04月21日 08:12:21 +00:00Commented Apr 21, 2011 at 8:12
For someone who is just picking up the language, I'd say you're off to a great start. Don't be afraid to abstract the different pieces of your program logic to many separate files and including them only when needed - It will make future revisions much less of a headache. When you are dealing with large blocks of output data like in your switch statement, PHP's output buffer can be your greatest friend. Your program also presents a great opportunity for you to get acquainted with the DOMDocument
extension - its relative purpose is structured output. Referencing what @php-coder suggested, once you get comfortable working with a database, you can start abstracting your application into purpose-driven layers - The Model-View-Controller design pattern is a pretty common method among popular PHP frameworks. PHP 5.3 and its common extensions host roughly 1,300 built-in functions - Have fun with it; it's a great language! +1 for your first review.
Edit:
How to style your code is normally a matter of personal preference and readability, but if you are looking for some general guidance, take a look at the Zend Framework Coding Style Guidelines. It's mostly just a reference for ZF extension authors, but it's none the less helpful for general interests.
-
\$\begingroup\$ Thanks for the vote of confidence. Will have a peek at the DOMDocument extension. Is it part of PHP? \$\endgroup\$PeanutsMonkey– PeanutsMonkey2011年04月21日 08:13:24 +00:00Commented Apr 21, 2011 at 8:13
-
\$\begingroup\$ I haven't seen an instance of PHP5 installed without it. \$\endgroup\$65Fbef05– 65Fbef052011年04月21日 11:26:50 +00:00Commented Apr 21, 2011 at 11:26