File size: 1,513 Bytes
56fb0d1
 
 
 
b54e722
 
 
 
 
 
 
 
 
 
 
 
 
 
56fb0d1
b54e722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56fb0d1
b54e722
 
 
 
56fb0d1
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
45
46
<?php
header("Content-Type: application/json");
include "db.php";

try {
    // Get menu items with stock and popularity (order count)
    $sql = "SELECT f.*,
            COALESCE(s.quantity, 0) as stock,
            COALESCE(pop.total_qty, 0) as popularity
            FROM foods f
            LEFT JOIN stock s ON f.id = s.food_id
            LEFT JOIN (
                SELECT food_id, SUM(quantity) as total_qty
                FROM order_items
                GROUP BY food_id
            ) pop ON f.id = pop.food_id
            ORDER BY popularity DESC";
    $stmt = $conn->query($sql);

    $menu = [];
    while ($row = $stmt->fetch()) {
        $row['id'] = (int)$row['id'];
        $row['price'] = (float)$row['price'];
        $row['stock'] = (int)$row['stock'];
        $row['popularity'] = (int)$row['popularity'];
        $cat_emojis = [
            'Beverages' => '🥤',
            'Wafers' => '🍪',
            'Snacks' => '🍟',
            'Coffee & Drinks' => '☕',
            'Hot Dogs' => '🌭',
            'Biryani' => '🍛'
        ];
        $row['emoji'] = $cat_emojis[$row['category']] ?? '🍽';
        $row['cat'] = $row['category'];
        $row['img'] = $row['image_url'];
        $row['sub'] = $row['description'];
        $menu[] = $row;
    }

    echo json_encode(['status' => 'success', 'count' => count($menu), 'data' => $menu]);
} catch (PDOException $e) {
    echo json_encode(['status' => 'error', 'message' => 'Query failed', 'error' => $e->getMessage()]);
}
?>