Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Php array comparison and compute

$
0
0

Hi I have these codes that try to compare content of array elements and add 1 for every similar comparison and zero for otherwise. But my codes seem not to be computing the final score, what could be the problem with my codes? Here are the codes:

<

 

<html>
<head>
<title>Chosen Answers</title>
</head>
<body>
<pre>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
 
//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"));
trim($fileContents);
fclose($openFile);
$delimiter = " ";
$myArray = explode($delimiter, $fileContents);
$myArray_trimmed = array_map('trim', $myArray);
print_r($myArray);
 
echo '*' . $answers[0] . '*' . $myArray[0] . '*';
$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>

>

 

What might be wrong with my codes? Thank you


Viewing all articles
Browse latest Browse all 13200

Trending Articles