<?php
require_once '../db.php';


$categories = [];
$catQuery = $conn->query("SELECT * FROM menu_categories ORDER BY id ASC");

while ($cat = $catQuery->fetch_assoc()) {
  $catId = $cat['id'];
  $items = [];
  $itemQuery = $conn->prepare("SELECT * FROM menu_items WHERE category_id = ?");
  $itemQuery->bind_param("i", $catId);
  $itemQuery->execute();
  $result = $itemQuery->get_result();
  while ($row = $result->fetch_assoc()) {
    $items[] = $row;
  }
  $categories[] = [
    "id" => $cat['id'],
    "name" => $cat['name'],
    "slug" => $cat['slug'],
    "items" => $items
  ];
  
}

echo json_encode($categories);
