summaryrefslogtreecommitdiff
path: root/api_cocktails.php
blob: 3ccde2b6330d1a7d04477c7a41e3c5eaa776b083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
header('Content-Type: application/json; charset=utf8mb4');
require 'db_config.php';

$stmt = $pdo->query("
    SELECT 
        c.*, 
        z.menge_ml, z.menge_anteil, z.einheit, z.original_text,
        z.zutat, z.zutat_de, z.kategorie, z.id as zutat_id
    FROM cocktails c 
    LEFT JOIN zutaten z ON c.id = z.cocktail_id 
    WHERE hide = 0
    ORDER BY c.seite, c.name, z.menge_ml DESC, z.id ASC
");

$cocktails = [];
$currentCocktail = null;

foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
    $cocktailId = $row['id'];
    
    if ($currentCocktail !== $cocktailId) {
        $currentCocktail = $cocktailId;
        $row['zutaten'] = [];
        $cocktails[] = $row;
    }
    
    // Zutat zu aktuellem Cocktail hinzufügen
    if (!empty($row['zutat_id'])) {
        $zutat = [
            'menge_ml' => $row['menge_ml'],
            'menge_anteil' => $row['menge_anteil'],
            'einheit' => $row['einheit'],
            'original_text' => $row['original_text'],
            'zutat' => $row['zutat'],
            'zutat_de' => $row['zutat_de'],
            'kategorie' => $row['kategorie'],
            'id' => $row['zutat_id']
        ];
        $cocktails[array_key_last($cocktails)]['zutaten'][] = $zutat;
    }
}

echo json_encode($cocktails);