Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
<?php
/**
* API Endpoint: Haal woorden op uit de database
* GET parameters:
* - category: (optioneel) Filter op categorie ID
* - difficulty: (optioneel) Filter op moeilijkheidsgraad
* - limit: (optioneel) Maximum aantal woorden (standaard 10)
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
require_once 'config.php';
try {
$pdo = getDbConnection();
// Haal parameters op
$category = isset($_GET['category']) ? intval($_GET['category']) : null;
$difficulty = isset($_GET['difficulty']) ? $_GET['difficulty'] : null;
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 10;
// Bouw de query
$query = "SELECT w.word_id, w.english_word, w.dutch_translation, w.difficulty_level,
c.category_name
FROM words w
JOIN categories c ON w.category_id = c.category_id
WHERE 1=1";
$params = [];
if ($category !== null) {
$query .= " AND w.category_id = :category";
$params[':category'] = $category;
}
if ($difficulty !== null && in_array($difficulty, ['beginner', 'intermediate', 'advanced'])) {
$query .= " AND w.difficulty_level = :difficulty";
$params[':difficulty'] = $difficulty;
}
$query .= " ORDER BY RAND() LIMIT :limit";
$stmt = $pdo->prepare($query);
// Bind parameters
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
$words = $stmt->fetchAll();
echo json_encode([
'success' => true,
'data' => $words,
'count' => count($words)
]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode([
'success' => false,
'error' => 'Failed to fetch words: ' . $e->getMessage()
]);
}