patternphpMinor
PHP Questionnaire Code Peer Review
Viewed 0 times
questionnairephppeercodereview
Problem
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 . '';
}
}
//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)
```
//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 . '';
}
}
//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)
Solution
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
- 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!)
Context
StackExchange Code Review Q#2004, answer score: 3
Revisions (0)
No revisions yet.