0

The two arrays does not compare and compute by adding one for every similar and 0 if otherwise I don't know what is wrong with the codes, but to me is seems fine. What might be the problem?

<html>
<head>
<title>Chosen answers</title>
</head>
<body>
<pre>
<?php
//Posting of the chosen answers

$answers = $_POST['selected_answers'];
echo '<b><u>THE ANSWERS YOU HAVE CHOSEN ARE:</u></b><br /><br />';
print_r($answers);

//Opening of the answers file, reading and printing it

$openFile = fopen("answers.txt", "r") or exit ("unable to open the 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 for the answered questions

if ($answers[0] == $myArray[0])
{
    $score = 1;
}
if ($answers[1] == $myArray[1])
{
    $score1 = 1;
}
if ($answers[2] == $myArray[2])
{
    $score2 = 1;
}
if ($answers[3] == $myArray[3])
{
    $score3 = 1;
}
if ($answers[4] == $myArray[4])
{
    $score4 = 1;
}

if ($answers[5] == $myArray[5])
{
    $score5 = 1;
}

if ($answers[6] == $myArray[6])
{
    $score6 = 1;
}

if ($answers[7] == $myArray[7])
{
    $score7 = 1;
}
if ($answers[8] == $myArray[8])
{
    $score8 = 1;
}

$Total = $score + $score1 + $score2 + $score3 + $score4 + $score5 + $score6 + $score7 + $score8 ;
echo '<br />';
echo "<b><u>$Total</u></b>";
?>
</pre>
</body>
</html>
2
Contributors
1
Reply
23
Views
4 Years
Discussion Span
Last Post by diafol
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.