<?php
// Enable error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Enable CORS headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json; charset=UTF-8");
// Include database connection
require_once 'db.php';
// Check if user_id is provided
if (!isset($_GET['user_id']) || empty($_GET['user_id'])) {
http_response_code(400);
echo json_encode([
'status' => 'error',
'message' => 'User ID is required'
]);
exit;
}
try {
// Sanitize user ID
$userId = intval($_GET['user_id']);
// Prepare SQL to fetch user's trainings with full training details
$stmt = $pdo->prepare("
SELECT
tp.id AS registration_id,
t.id AS training_id,
t.title,
t.date,
t.time,
t.location,
t.description,
t.fees,
t.trainer_name,
t.training_type,
t.difficulty_level,
t.category,
t.training_image,
tp.registration_date,
tp.payment_status
FROM
training_participants tp
JOIN
trainings t ON tp.training_id = t.id
WHERE
tp.user_id = :user_id
ORDER BY
t.date DESC
");
$stmt->execute(['user_id' => $userId]);
$trainings = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Prepare response
$response = [
'status' => 'success',
'message' => $trainings ? 'Trainings retrieved successfully' : 'No trainings found',
'total_trainings' => count($trainings),
'trainings' => $trainings
];
echo json_encode($response);
} catch (Exception $e) {
http_response_code(500);
echo json_encode([
'status' => 'error',
'message' => 'Error fetching trainings: ' . $e->getMessage()
]);
}
?>