0

This is my goal trying to remove duplicates and also keep matched from the exploded value and the $classQuery statement selected

enter image description here

$examQuery = $examClass->get_examByID($id);

$examRow = $examQuery->fetch_assoc();

$classes = explode(',', $examRow['class_id']);

$classQuery2 = $con->query("SELECT * FROM `class` WHERE `school_id` = '{$examRow['school_id']}' ");
if ($classQuery2->num_rows < 1):
    $output .= '';
else:

    while ($class_rows = $classQuery2->fetch_assoc()):
        foreach ($classes as $class):
            if ($class_rows['class_id'] === $class):
                $output .= '<option selected value="' . $class_rows['class_id'] . '">' . $class_rows['class_title'] . '</option>';
            else:
                $output .= '<option value="' . $class_rows['class_id'] . '">' . $class_rows['class_title'] . '</option>';
            endif;
        endforeach;
    endwhile;
endif;
3
  • 1
    Are you comparing the class_id (I suppose that It's integer Type) with $class which type is Object ?? In line 14: if ($class_rows['class_id'] === $class):
    – R. García
    Jul 20, 2018 at 6:08
  • $classes is an array of IDs and consequently $class is an id. So that part should be OK ... Jul 20, 2018 at 6:14
  • but am getting duplicates which i don't want Jul 20, 2018 at 6:16

2 Answers 2

1

You should be able to resolve this simply by putting a GROUP BY on your query;

$classQuery2 = $con->query("SELECT * FROMclassWHEREschool_id= '{$examRow['school_id']}' GROUP BY class_id");

Otherwise, I'd probably keep an array of ones already used and check if the key is in that before echoing;

For example;

// the array of items to loop over (which has a duplicate in it)
$myArray = ['english', 'maths', 'science', 'geography', 'english'];

// an array to place the item name when it's been used once
$alreadyUsed = [];

foreach($myArray as $item) {
    // if the item doesn't already exist in $alreadyUsed then 
    // echo it out and add it to the array.
    if (! in_array($item, $alreadyUsed)) {
        echo $item . "<br>";
        $alreadyUsed[] = $item;
    }
}
7
  • Please am not getting it Jul 20, 2018 at 6:25
  • What are you not getting? Does that not work or do you not understand my answer?
    – Simon R
    Jul 20, 2018 at 6:27
  • Change your query to this: $classQuery2 = $con->query("SELECT * FROMclassWHEREschool_id= '{$examRow['school_id']}' GROUP BY class_id");
    – Simon R
    Jul 20, 2018 at 6:29
  • Then refresh in your browser
    – Simon R
    Jul 20, 2018 at 6:29
  • i have done as instructed but i think my problem is coming from the while loop and foreach loop ... please i beg u help me analyze my code u get my problem Jul 20, 2018 at 6:34
0

You are comparing different Types!

If you want a good comparasion, you'll need to compare the values with the same type
e.g Integer === Integer || String === String || Object === Object


In your case, you have to compare the $class_rows['class_id'] with your class ID,
Something like that:

if ($class_rows['class_id'] === $class.id):
            # Successful Condition
else:
            # Unsuccesful Condition
endif;
3
  • You haven't closed the if with endif;
    – Simon R
    Jul 20, 2018 at 6:19
  • Also what is $class.id? $class is an integer (well far more likely a string which just looks like a number). Since databases very often return a number as a string this OP's type comparison should have no issues.
    – Simon R
    Jul 20, 2018 at 6:22
  • Thanks R.Garcia the major problem is to remove the duplicates Jul 20, 2018 at 6:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.