35607-vm/_get_best_selling_products.php
Flatlogic Bot 9b5a06451f SInarKasih
2025-11-10 04:11:47 +00:00

27 lines
652 B
PHP

<?php
require_once __DIR__ . '/db/config.php';
try {
$pdo = db();
$stmt = $pdo->query("
SELECT
p.name,
SUM(si.quantity) as total_quantity
FROM sale_items si
JOIN products p ON si.product_id = p.id
GROUP BY p.name
ORDER BY total_quantity DESC
LIMIT 5
");
$best_selling_products = $stmt->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: application/json');
echo json_encode($best_selling_products);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
}
?>