comparison - Read questions and answers in an array and display -
comparing array elements , add 1 or 0 if arrays same or not. not adding up, doing wrong? because arrays not seem comparing.
<html> <head> <title>chosen answers</title> </head> <body> <pre> <?php //posting of chosen answers $answers = $_post['selected_answers']; echo '<b><u>the answers have chosen are:</u></b><br /><br />'; print_r($answers); //opening of answers file, reading , printing $openfile = fopen("answers.txt", "r") or exit ("unable open answers file"); $filecontents = fread($openfile, filesize("answers.txt")); fclose($openfile); $delimiter = " "; $myarray = explode($delimiter, $filecontents); $score = $score1 = $score2 = $score3 = $score4 = $score5 = $score6 = $score7 = $score8 = 0; //computation of marks scored answered questions if ($answers[0] == $myarray[0]) { $score = 1; } elseif ($answers[0] !=$myarray[0]) { $score = 0; }echo '<br />'; if ($answers[1] == $myarray[1]) { $score1 = 1; } elseif ($answers[1] !=$myarray[1]) { $score1 = 0; }echo '<br />'; if ($answers[2] == $myarray[2]) { $score2 = 1; } elseif ($answers[2] !=$myarray[2]) { $score2 = 0; }echo '<br />'; if ($answers[3] == $myarray[3]) { $score3 = 1; } elseif ($answers[3] !=$myarray[3]) { $score3 = 0; }echo '<br />'; if ($answers[4] == $myarray[4]) { $score4 = 1; } elseif ($answers[4] !=$myarray[4]) { $score4 = 0; }echo '<br />'; if ($answers[5] == $myarray[5]) { $score5 = 1; } elseif ($answers[5] !=$myarray[5]) { $score5 = 0; }echo '<br />'; if ($answers[6] == $myarray[6]) { $score6 = 1; } elseif ($answers[6] !=$myarray[6]) { $score6 = 0; }echo '<br />'; if ($answers[7] == $myarray[7]) { $score7 = 1; } elseif ($answers[7] !=$myarray[7]) { $score7 = 0; }echo '<br />'; if ($answers[8] == $myarray[8]) { $score8 = 1; } elseif ($answers[8] !=$myarray[8]) { $score8 = 0; } $total = $score + $score1 + $score2 + $score3 + $score4 + $score5 + $score6 + $score7 + $score8 ; echo "<b><u>$total</u></b>"; ?> </pre> </body> </html>
how can able compare 2 arrays , compute total marks. first array $answers submitted form. , second 1 read text file called answers.txt
your code redundant should optimize it. try it
<?php $total=0; if ($answers[1]=="data coupling") { $total++; } else { print_r($answers[1]); } //third question if ($answers[2]=="unit testing") { $total++; } else { print_r($answers[2]); } //fourth question if ($answers[3]=="mutation testing") { $total++; } else { print_r($answers[3]); } //fifth question if ($answers[4]=="white box testing") { $total++; } else { print_r($answers[4]); } //sixth question if ($answers[5]=="ad hoc") { $total++; } else { print_r($answers[5]); } //seventh question if ($answers[6]=="information domain values") { $total++; } else { print_r($answers[6]); } //eigth question if ($answers[7]=="functional , behavioral") { $total++; } else { print_r($answers[7]); } //nineth question if ($answers[8]=="efficiency") { $total++; } else { print_r($answers[8]); } //code generate total score echo $total; ?>