Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/cid/system/
Upload File :
Current File : C:/xampp/htdocs/cid/system/get_plan_data.php

<?php
@include 'include/config.php';
session_start();

header('Content-Type: application/json');

if (!isset($_SESSION['username_school'])) {
    echo json_encode(["error" => "User not logged in"]);
    exit;
}

$username = $_SESSION['username_school'];
$month = $_GET['month'] ?? '';

if (empty($month)) {
    echo json_encode(["error" => "Month parameter is missing"]);
    exit;
}

// Updated query: Join mista_plan_activities with mista_plans based on the correct columns
$sql = "SELECT a.date, a.school, a.school_head, a.instructional_supervisory_and_monitoring_activity
        FROM mista_plan_activities a
        JOIN mista_plans p ON a.plan_id = p.id  -- 'a.plan_id' (in mista_plan_activities) is linked to 'p.id' (in mista_plans)
        WHERE p.username = ? AND p.month = ?";

$stmt = $conn->prepare($sql);

if (!$stmt) {
    echo json_encode(["error" => "Prepare failed: " . $conn->error]);
    exit;
}

$stmt->bind_param("ss", $username, $month);
$stmt->execute();
$result = $stmt->get_result();

// Check if the query returns any rows
if ($result->num_rows > 0) {
    $rows = [];
    while ($row = $result->fetch_assoc()) {
        $rows[] = [
            "date" => $row["date"],
            "school" => $row["school"],
            "school_head" => $row["school_head"],
            "activity" => $row["instructional_supervisory_and_monitoring_activity"]
        ];
    }
    echo json_encode($rows);
} else {
    echo json_encode(["error" => "No data found for the selected month"]);
}