-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_product.php
More file actions
61 lines (49 loc) · 1.74 KB
/
fetch_product.php
File metadata and controls
61 lines (49 loc) · 1.74 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
<?php
include("database.php");
$lastProductID = isset($_GET['lastProductID']) ? intval($_GET['lastProductID']) : 0;
$randomMode = isset($_GET['randomMode']) && $_GET['randomMode'] == 'true';
$productType = isset($_GET['productType']) ? $_GET['productType'] : null; // New parameter for product type
$limit = 30;
// Start building the base query
$query = "SELECT * FROM products WHERE Stock > 0";
// Modify the query based on product type
$params = [];
if ($productType) {
// If product type is not null, filter by that type
$query .= " AND ProductType = ?";
$params[] = $productType; // Add product type to params for binding
} else {
// If product type is null, fetch both coffin and urn
$query .= " AND ProductType IN ('coffin', 'urn')";
}
// If lastProductID is provided and not in random mode, add to query
if (!$randomMode && $lastProductID > 0) {
$query .= " AND ProductID > ?";
$params[] = $lastProductID; // Add lastProductID to params for binding
}
// Modify the query to use random order if randomMode is true
if ($randomMode) {
$query .= " ORDER BY RAND()";
} else {
$query .= " ORDER BY ProductID ASC";
}
$query .= " LIMIT $limit";
// Prepare the statement
$stmt = $connect->prepare($query);
// Bind parameters based on the number of params
if (!empty($params)) {
// Create the binding types string
$bindingTypes = str_repeat("s", count($params)); // Assuming all are strings
$stmt->bind_param($bindingTypes, ...$params);
}
// Execute the statement
$stmt->execute();
$result = $stmt->get_result();
$products = [];
while ($row = $result->fetch_assoc()) {
$products[] = $row;
}
// Set the content type and return the JSON response
header('Content-Type: application/json');
echo json_encode($products);
?>