Invalid test ID.
"; require_once 'includes/footer.php'; exit(); } $test_id = intval($_GET['test_id']); $user_id = $_SESSION['user_id']; try { $db = db(); // Check if user has already taken this test $checkStmt = $db->prepare("SELECT COUNT(*) FROM user_tests WHERE user_id = :user_id AND test_id = :test_id"); $checkStmt->execute(['user_id' => $user_id, 'test_id' => $test_id]); if ($checkStmt->fetchColumn() > 0) { echo "
You have already completed this test. View your results.
"; require_once 'includes/footer.php'; exit(); } // Fetch test details $stmt = $db->prepare("SELECT title, description FROM tests WHERE id = :id"); $stmt->execute(['id' => $test_id]); $test = $stmt->fetch(PDO::FETCH_ASSOC); if (!$test) { echo "
Test not found.
"; require_once 'includes/footer.php'; exit(); } // Fetch questions and options $questionsStmt = $db->prepare(" SELECT q.id AS question_id, q.question_text, o.id AS option_id, o.option_text, o.score FROM questions q JOIN options o ON q.id = o.question_id WHERE q.test_id = :test_id ORDER BY q.id, o.id "); $questionsStmt->execute(['test_id' => $test_id]); $questionsData = $questionsStmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC); $question_keys = array_keys($questionsData); } catch (PDOException $e) { die("Database error: " . $e->getMessage()); } ?>


This test has no questions yet.

$question_id): $options = $questionsData[$question_id]; $is_first = ($index === 0); $is_last = ($index === count($question_keys) - 1); ?>