27 lines
827 B
PHP
27 lines
827 B
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
$recipes_stmt = $pdo->query('SELECT * FROM recipes ORDER BY created_at DESC');
|
|
$recipes = $recipes_stmt->fetchAll();
|
|
|
|
$ingredients_stmt = $pdo->query('SELECT * FROM ingredients');
|
|
$all_ingredients = $ingredients_stmt->fetchAll();
|
|
|
|
$ingredients_by_recipe = [];
|
|
foreach ($all_ingredients as $ingredient) {
|
|
$ingredients_by_recipe[$ingredient['recipe_id']][] = $ingredient;
|
|
}
|
|
|
|
foreach ($recipes as $i => $recipe) {
|
|
$recipes[$i]['ingredients'] = $ingredients_by_recipe[$recipe['id']] ?? [];
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'recipes' => $recipes]);
|
|
|
|
} catch (PDOException $e) {
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|