prepare($query);
$stmt->bind_param("i", $programID);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$courseID = $row["Course_ID"];
// Check if an unassigned schedule exists for this course and group
$checkStmt = $mysqli->prepare("SELECT COUNT(*) FROM Course_Group_Schedule_Main
WHERE Program_ID = ? AND Course_ID = ? AND Group_ID = ? AND Assigned = 0");
if (!$checkStmt) {
die("Error preparing assignment check query: " . $mysqli->error);
}
$checkStmt->bind_param('iii', $programID, $courseID, $groupID);
$checkStmt->execute();
$checkStmt->bind_result($count);
$checkStmt->fetch();
$checkStmt->close();
// Only process the course if there is an unassigned schedule record.
if ($count > 0) {
// Fetch the schedule details for this course
$fetchStmt = $mysqli->prepare("SELECT Reserve_Course, Time_Slot, Start_Date, End_Date, Start_Time, End_Time
FROM Course_Group_Schedule_Main
WHERE Program_ID = ? AND Course_ID = ? AND Group_ID = ? AND Assigned = 0");
if (!$fetchStmt) {
echo "Error preparing schedule fetch query for Course ID $courseID: " . $mysqli->error . " ";
continue;
}
$fetchStmt->bind_param('iii', $programID, $courseID, $groupID);
$fetchStmt->execute();
$fetchStmt->bind_result($Reserve_Course, $Time_Slot, $Start_Date, $End_Date, $Start_Time, $End_Time);
if (!$fetchStmt->fetch()) {
echo "No unassigned schedule found for Course ID $courseID. ";
$fetchStmt->close();
continue;
}
$fetchStmt->close();
// Prepare the Reserve_Course checkbox value
$checkedReserve = ($Reserve_Course == 1) ? 'checked' : '';
// If a teacher is selected, check for conflicts with existing teacher assignments.
if ($teacherID > 0) {
$conflictStmt = $mysqli->prepare("SELECT COUNT(*) FROM Teacher_Course_Assignments
WHERE Teacher_ID = ?
AND Time_Slot = ?
AND (? <= End_Date AND ? >= Start_Date)");
if ($conflictStmt) {
$conflictStmt->bind_param("isss", $teacherID, $Time_Slot, $Start_Date, $End_Date);
$conflictStmt->execute();
$conflictStmt->bind_result($conflictCount);
$conflictStmt->fetch();
$conflictStmt->close();
// If there is a conflict, skip this course row.
if ($conflictCount > 0) {
continue;
}
}
}
// Output the row for this course.
echo "