0

I am having problem with my nested foreach loop, I have this code of my controller retrieving the values of two columns in separate table in my database.

What I want is to compare each values against the other table vice versa,...

table1             table2
some column1       some column2 
 a                  b
 b                  b
 c                  c

My desired output would be if the values of two columns compare, if it is true then output "match" otherwise "mismatch".

Here the attempt, but it doesn't work, only the last item in both tables column are being compared. i think i am missing with my nested loops.

///snippet///

controller

$temp_answers = array();
$answers = array();

$temp_answers = Tempanswer::where('subject_id', $subject_id)
                             ->where('student_id', $student_id)
                             ->lists('temp_answer');

$answers = Question::where('subject_slug', $subject->slug)
                             ->lists('letteranswer');

foreach ($temp_answers as $temp_answer) {

    foreach ($answers as $answer) {
        if($answer == $temp_answer){
            $flag = 'match';
        }else 
            $flag = 'mismatch';
    }
    echo $flag.' ';

 }
6
  • $flag variable overwrites on each foreach iteration. Use array to store flags Commented Aug 29, 2015 at 15:24
  • Change $flag = 'match'; to a simple echo 'match'; also do this for the mismatch and loose the echo $flag; If you layout your code with sensible indentation like above, the error becomes so much easier to see Commented Aug 29, 2015 at 15:28
  • it was for debugging porpuses, that not actually the real score, theres my pivot table i want to insert something base on the result of nested loops Commented Aug 29, 2015 at 15:32
  • @RiggsFolly is my nested foreach correct sir? despite of those variables? Commented Aug 29, 2015 at 15:34
  • Basically you're comparing every given answer with every possible answer. That doesn't really make sense, there's no point in comparing the given answer for question 4 to the actual answer of question 2. Commented Aug 29, 2015 at 17:22

2 Answers 2

3

CMIIW. You want to check table1 and table2 is match. So if its not match you will get message "mismatch"

Assumption 1: if each data from table1 is compared with all data in table2.

foreach ($temp_answers as $temp_answer) {

    foreach ($answers as $answer) {
        if($answer == $temp_answer){
            $flag = 'match';
        }else {
            $flag = 'mismatch';
            break;
        }
    }

    if($flag == 'mismatch'){
        break;
    }  
 }

echo $flag;

Assumption 2: Each table is compared by each row. I mean row1 table1 is compared with row1 table2 and than row2 table1 is compared with row2 table2.

$flag='';
foreach ($temp_answers as $key1=>$temp_answer) {
    foreach ($answers as $key2=>$answer) {
        if($key1 == $key2){
            if($answer == $temp_answer){
                $flag = $flag.'match ';
            }else {
                $flag = $flag.'mismatch ';
            }
            break;
        }
    }
}
echo $flag;
Sign up to request clarification or add additional context in comments.

22 Comments

hmmm... i wanna ask. you want 'match' message returned if all data in column of each table is same. once data is different, you want message returned is 'mismatch'. am i right?
MISMATCH MISMATCH MISMATCH
assuming 3 data are being compared its tables
what i mean is the primary key/foreign on the other table, the value on that column matched the other echo match otherwise mismatch?
i have updated it. please check if that is what you want.
|
1

Well I would have done it like this.

First give variables names that help you know what is in them

Second always use {} in if else even if there is only one line in the if or else, it makes it easier to see where things actually start and finish.

Then always use indentation as well so you can visually see where a block of code starts and finishes. Remember, you may have to come back to a piece of code weeks after writing it, so make it easy to read as it may be you that has to work out what you did.

Also when you output info about what matches or mismatches also include the data you need to see in order to tell that the matching is correct. Then if it does not do quite what you want you can easily see whats going wrong.

$temp_answers = array();
$questions = array();

$temp_answers = Tempanswer::where('subject_id', $subject_id)
                             ->where('student_id', $student_id)
                             ->lists('temp_answer');

$questions = Question::where('subject_slug', $subject->slug)
                             ->lists('letteranswer');

foreach ($temp_answers as $temp_answer) {

    foreach ($questions as $question ) {

        if($question == $temp_answer){
            echo "Question = $question TempAnswer = $temp_answer > MATCH" . PHP_EOL;
        } else {
            echo "Question = $question TempAnswer = $temp_answer > MISMATCH" . PHP_EOL;
        }
    }

 }

2 Comments

but again why is it only the last item in my array being compared in both table? will i try to put the same data on both table but the result will always like this base on your code
MISMATCH MISMATCH MATCH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.