prepare("SELECT Group_ID, Time_Slot, Time_From, Time_To FROM Manager_Group_Name WHERE Program_ID = ? AND Group_ID = ?");
//$fetchStmt->bind_param('ii', $programID, $groupID);
//$fetchStmt->execute();
//$fetchStmt->bind_result($Time_Slot, $Time_From, $Time_To);
//$fetchStmt->fetch();
//$fetchStmt->close();
//
//$timeSlotQuery = "SELECT Time_Slot_ID, Time_Slot, Time_From, Time_To FROM Time_Slot_Programs";
//$timeSlotQuery->bind_param('ii', $programID, $groupID);
//$timeSlotQuery->execute();
//$timeSlotQuery->bind_result($Time_Slot_ID, $Time_Slot, $Time_To, $Time_From, $Time_To);
//$timeSlotQuery->fetch();
//$timeSlotQuery->close();
/// 1. Retrieve the group record from Manager_Group_Name.
$groupQuery = "SELECT Group_ID, Time_Slot, Time_From, Time_To
FROM Manager_Group_Name
WHERE Program_ID = ? AND Group_ID = ?";
$stmtGroup = $mysqli->prepare($groupQuery);
if (!$stmtGroup) {
die("Error preparing group query: " . $mysqli->error);
}
$stmtGroup->bind_param("ii", $programID, $groupID);
$stmtGroup->execute();
$stmtGroup->bind_result($dbGroupID, $groupTimeSlot, $groupTimeFrom, $groupTimeTo);
$stmtGroup->fetch();
$stmtGroup->close();
if (!$groupTimeSlot) {
die("No time slot information found for the group.");
}
// For example, $groupTimeSlot should now be "Morning,Afternoon".
// 2. Split the comma-separated string into an array.
$timeSlotsArray = array_map('trim', explode(',', $groupTimeSlot));
// 3. Build a quoted string for the SQL IN clause.
// For example, if $timeSlotsArray is ["Morning", "Afternoon"], this builds: 'Morning','Afternoon'
$quotedSlots = implode(',', array_map(function($s) use ($mysqli) {
return "'" . $mysqli->real_escape_string($s) . "'";
}, $timeSlotsArray));
// 4. Retrieve only the time slots from Time_Slot_Programs that match those in the group.
$timeSlotQuery = "SELECT Time_Slot_ID, Time_Slot, Time_From, Time_To
FROM Time_Slot_Programs
WHERE Time_Slot IN ($quotedSlots)";
$resultTimeSlot = $mysqli->query($timeSlotQuery);
if (!$resultTimeSlot) {
die("Error executing time slot query: " . $mysqli->error);
}
// Fix: ORDER BY clause added to the query
$query = "SELECT Course_ID, Course_Name FROM Courses WHERE Program_ID = ? ORDER BY Course_ID";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("i", $programID);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$courseID = $row["Course_ID"];
// Check if the combination of Program_ID, Course_ID, and Group_ID exists in Course_Group_Schedule
$checkStmt = $mysqli->prepare("SELECT COUNT(*) FROM Course_Group_Schedule WHERE Program_ID = ? AND Course_ID = ? AND Group_ID = ?");
$checkStmt->bind_param('iii', $programID, $courseID, $groupID);
$checkStmt->execute();
$checkStmt->bind_result($count);
$checkStmt->fetch();
$checkStmt->close();
// If combination exists in Course_Group_Schedule
if ($count > 0) {
// Fetch additional schedule details for this combination
$fetchStmt = $mysqli->prepare("SELECT Time_Slot_ID, Reserve_Course, Time_Slot, Start_Date, End_Date, Start_Time, End_Time FROM Course_Group_Schedule WHERE Program_ID = ? AND Course_ID = ? AND Group_ID = ?");
$fetchStmt->bind_param('iii', $programID, $courseID, $groupID);
$fetchStmt->execute();
$fetchStmt->bind_result($Time_Slot_ID, $Reserve_Course, $Time_Slot, $Start_Date, $End_Date, $Start_Time, $End_Time);
$fetchStmt->fetch();
$fetchStmt->close();
// Assuming $Reserve_Course is a variable that holds the value (either 0 or 1)
$checkedReserve = (isset($Reserve_Course) && $Reserve_Course == 1) ? 'checked' : ''; // Set checked if $Reserve_Course is 1
$Start_Time = date('h:i A', strtotime($Start_Time));
$End_Time = date('h:i A', strtotime($End_Time));
echo "
{$row["Course_Name"]}
";
} else {
// If the combination doesn't exist in Course_Group_Schedule
echo "