Jump to content

Php array comparison and compute


Go to solution Solved by Ferdinand,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/277446-php-array-comparison-and-compute/
Share on other sites

Well what do you think the problem is? When you print out $answers and $myArray are you getting the arrays that you think you are supposed to be getting? If that's the case, then is $myArray holding special characters?

 

I see you do $myArray_trimmed = array_map('trim', $myArray); But then you don't use $myArray_trimmed, you go back to using $myArray.

What output ARE you getting?

 

Nothing jumps out as being incorrect so, like akphidelt2007 is saying, I don't think the data is exactly what you expect.

 

Also, you could really clean up the logic with a simply loop:

$Total = 0;
foreach ($answers as $i => $answer)
{
	if ($answer == $myArray[$i])
		$Total++;
}

I have changed it as follows, but its still not computing the score, I don't know why

<

 

<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 />';
$answers_trimmed = array_map('trim', $answers);
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);
//Deletes unnecessary spaces in the read text files.
$myArray_trimmed = array_map('trim', $myArray);
print_r($myArray_trimmed);
 
echo '*' . $answers[0] . '*' . $myArray_trimmed[0] . '*';
//Computation of marks scored for the answered questions
 
$Total = 0;
foreach ($answers as $i => $answer)
{
if ($answer == $myArray_trimmed[$i])
$Total++;
}
echo '<br /><br />';
echo "<b><u>$Total</u></b>";
?>
</pre>
</body>
</html>

 and the score is 0

>

  • Solution

Thanks to you all who have assisted me, Lemmin you gave me the idea and this is how I have implemented it


$score = 0;
$no_questions = 9;

for ($x = 0; $x < $no_questions; $x++) 
{
if ($answers_trimmed [$x]== $myArray_trimmed[$x])
				{
				$score++;
				}
}
echo '<br />';
echo "<b><u>$score</u></b>";
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.