i need to check if in an exam that contains 5 students
exist 3 students from the same class.
here is what i tried
<?
//this array contains all student id's that are in an exam
$exam = array('s1' => $s1, 's2' => $s2, 's3' => $s3, 's4' => $s4, 's5' => $s5);
$values = implode(", ", $exam);
$sql = "SELECT class FROM students WHERE students.id IN (" . $values . ")";
try{
$db = new db();
$db = $db->connect();
$stmt = $db->query($sql);
$studs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$db = null;
if(!empty($studs)) {
//check if 3 students from the same class are taking the exam
$i = 0; $s = 0;
foreach($exam as $e )
{
if( !in_array( $e, $studs[$i] ) )
{
$exist = FALSE;
}
else {$s++;}
$i++;
}
if ($s<=3) {
#do sth
}
else {
echo "more than 3 students";
}
} else {
echo "error";
}
} catch(PDOException $e) {}
?>
Problem what i am not sure about is how to count that 3 students have the same class id in this exam array.
i know there is something i need to fix in my foreach just trying with no success.