-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts.php
49 lines (40 loc) · 1.41 KB
/
products.php
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
47
48
49
<?php
include "includes/db_bangiay.inc";
header('Content-Type: application/json');
$products_per_page = 8;
// Lấy trang từ request
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$category_id = isset($_GET['category']) ? $_GET['category'] : 'all';
// Tính total và offset
$sql_count = "SELECT COUNT(*) AS total FROM sanpham";
if ($category_id !== 'all') {
$sql_count .= " WHERE MaLoaiSanPham = '$category_id'";
}
$result_count = $conn->query($sql_count);
$total_products = $result_count->fetch_assoc()['total'];
$total_pages = ceil($total_products / $products_per_page);
$offset = ($page - 1) * $products_per_page;
// Query sản phẩm
$sql_products = "SELECT * FROM sanpham";
if ($category_id !== 'all') {
$sql_products .= " WHERE MaLoaiSanPham = '$category_id'";
}
$sql_products .= " LIMIT $products_per_page OFFSET $offset";
$result_products = $conn->query($sql_products);
$products = [];
while ($row = $result_products->fetch_assoc()) {
$products[] = $row;
}
// Query categories
$sql_categories = "SELECT * FROM loaisanpham";
$result_categories = $conn->query($sql_categories);
$categories = [];
while ($row = $result_categories->fetch_assoc()) {
$categories[$row['MaLoaiSanPham']] = $row['TenLoaiSanPham'];
}
echo json_encode([
'products' => $products,
'categories' => $categories,
'totalPages' => $total_pages,
'currentPage' => $page
]);