I have two tables in my database: students and teachers. I am trying to assign each teacher with 4 students that are writing their essay on that teacher's subject. For that, I have created a third table: assignment. I made two conditions: if the student is assigned (his or her assigned table value is 1), then the code should skip that student AND if the teacher has more than 3 assigned students (his or her assigned table value is less than 4), then the code should skip that teacher. This is the code that I have tried:
<?php
require('config_teachers.php');
require('config_students.php');
// We get all the info as associative arrays: $students_info and $teachers_info.
$serverName = "localhost";
$user = "root";
$password = "";
$dbName = "yerkindb";
foreach($students_info as $student_info){
foreach($teachers_info as $teacher_info){
if($student_info['assigned'] == 0 && $student_info['ee_subject'] == $teacher_info['ee_subject_1_choice'] && $teacher_info['assigned'] < 4 ){
$conn = mysqli_connect($serverName, $user, $password, $dbName);
if(mysqli_connect_errno()){
echo "Failed to connect to mysqli.".mysqli_connect_errno();
} else {
$name_student = $student_info['name'];
$name_teacher = $teacher_info['name'];
$subject = $student_info['ee_subject'];
$query = "INSERT INTO assignment(student_name, advisor_name, subject) values('$name_student', '$name_teacher', '$subject')";
mysqli_query($conn, $query);
$assigned_teacher = $teacher_info['assigned'];
$assigned_teacher = $assigned_teacher + 1;
$query = "UPDATE teachers SET assigned = $assigned_teacher WHERE name = '$name_teacher'";
mysqli_query($conn, $query);
$query = "UPDATE students SET assigned = 1 WHERE name = '$name_student'";
mysqli_query($conn, $query);
mysqli_close($conn);
}
}
}
}
?>
The line
if($student_info['assigned'] == 0 && $student_info['ee_subject'] == $teacher_info['ee_subject_1_choice'] && $teacher_info['assigned'] < 4 ){
has the conditions that I want to assign, however, when I run the code, I get each student getting assigned to every single teacher that has the same subject as the subject he or she is writing her essay on.
Can someone explain why are those conditions getting skipped, even though the
$student_info['ee_subject'] == $teacher_info['ee_subject_1_choice']
condition is not getting overlooked.