Ferdinand Posted April 30, 2013 Share Posted April 30, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/277446-php-array-comparison-and-compute/ Share on other sites More sharing options...
akphidelt2007 Posted April 30, 2013 Share Posted April 30, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277446-php-array-comparison-and-compute/#findComment-1427282 Share on other sites More sharing options...
lemmin Posted April 30, 2013 Share Posted April 30, 2013 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/277446-php-array-comparison-and-compute/#findComment-1427283 Share on other sites More sharing options...
Ferdinand Posted April 30, 2013 Author Share Posted April 30, 2013 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 > Quote Link to comment https://forums.phpfreaks.com/topic/277446-php-array-comparison-and-compute/#findComment-1427287 Share on other sites More sharing options...
Solution Ferdinand Posted April 30, 2013 Author Solution Share Posted April 30, 2013 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/277446-php-array-comparison-and-compute/#findComment-1427297 Share on other sites More sharing options...
akphidelt2007 Posted April 30, 2013 Share Posted April 30, 2013 (edited) I'm glad you found a solution, but if that works... the previous foreach() loop solution should work also. Edited April 30, 2013 by akphidelt2007 Quote Link to comment https://forums.phpfreaks.com/topic/277446-php-array-comparison-and-compute/#findComment-1427302 Share on other sites More sharing options...
Ferdinand Posted April 30, 2013 Author Share Posted April 30, 2013 I thinks it was some errors in the syntax. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/277446-php-array-comparison-and-compute/#findComment-1427305 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.