-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct_report.php
86 lines (77 loc) · 3.11 KB
/
product_report.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
\<?php
require_once 'db.php';
// Initialize response array to store product data
$response = array('products' => array());
// Check if the search item and filter are provided via GET request
if (isset($_GET['search']) && isset($_GET['filter'])) {
// Sanitize and retrieve search item and filter from GET parameters
$search = $_GET['search'];
$filter = $_GET['filter'];
// Prepare SQL query based on the selected filter
switch ($filter) {
case 'name':
case 'description':
case 'category':
case 'brand':
// Handle basic filters (name, description, category, brand)
$sql = "SELECT * FROM products WHERE LOWER($filter) LIKE ?";
break;
case 'date_added':
// Filter by exact date (date added)
$sql = "SELECT * FROM products WHERE DATE(date_added) = ?";
break;
case 'date_day':
// Filter by day (extract day from date_added)
$sql = "SELECT * FROM products WHERE DAY(date_added) = ?";
break;
case 'date_month':
// Filter by month (extract month from date_added)
$sql = "SELECT * FROM products WHERE MONTH(date_added) = ?";
break;
case 'date_year':
// Filter by year (extract year from date_added)
$sql = "SELECT * FROM products WHERE YEAR(date_added) = ?";
break;
default:
// Invalid filter (handle appropriately)
$response['error'] = "Invalid filter specified.";
echo json_encode($response);
exit;
}
// Prepare and execute the SQL query with search term
$stmt = $conn->prepare($sql);
if ($stmt) {
$searchTerm = '%' . strtolower($search) . '%';
$stmt->bind_param("s", $searchTerm);
$stmt->execute();
// Get result set from the prepared statement
$result = $stmt->get_result();
// Iterate through each row of the result set and store product details in the response array
while ($row = $result->fetch_assoc()) {
$product = array(
'name' => $row['name'],
'category' => $row['category'],
'brand' => $row['brand'],
'unit' => $row['unit'],
'cost_per_unit' => $row['cost_per_unit'],
'description' => $row['description'],
'stock' => $row['stock'],
'date_added' => $row['date_added']
);
array_push($response['products'], $product);
}
// Close the prepared statement
$stmt->close();
} else {
// Error handling if the prepared statement fails
$response['error'] = "Error preparing SQL statement: " . $conn->error;
}
} else {
// Error handling for missing search item or filter
$response['error'] = "Missing search item or filter parameters.";
}
// Close the database connection
$conn->close();
// Encode the response array as JSON and output the result
echo json_encode($response);
?>