-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_items.php
More file actions
61 lines (48 loc) · 1.51 KB
/
search_items.php
File metadata and controls
61 lines (48 loc) · 1.51 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
// search_items.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lost_found_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get search and category parameters
$search = isset($_GET['search']) ? $_GET['search'] : '';
$category = isset($_GET['category']) ? $_GET['category'] : '';
// Updated SQL query to include img1
$sql = "SELECT id_item, item_name, location_found, img1, other_details
FROM reported_items
WHERE item_name LIKE ?
AND status = 'Unclaimed'
AND (remark = 'Approved' OR remark = 'Pending')";
// Array to hold parameters and their types
$params = [];
$types = "s"; // 's' for the search string
$params[] = "%$search%";
// Add category filter if provided
if (!empty($category)) {
$sql .= " AND item_category = ?";
$types .= "s"; // Add another string type for the category
$params[] = $category;
}
// Append ORDER BY clause
$sql .= " ORDER BY report_date DESC, report_time DESC";
// Prepare and bind parameters
$stmt = $conn->prepare($sql);
// Bind the parameters dynamically using call_user_func_array
$stmt->bind_param($types, ...$params);
// Execute and fetch the results
$stmt->execute();
$result = $stmt->get_result();
$items = [];
while ($row = $result->fetch_assoc()) {
$items[] = $row;
}
// Send the result as JSON
header('Content-Type: application/json');
echo json_encode($items);
$stmt->close();
$conn->close();
?>