-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_notice.php
53 lines (46 loc) · 1.74 KB
/
search_notice.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
<?php
include('php\database.php');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if at least one search criterion is provided
if (!empty($_POST['title']) || !empty($_POST['date']) || !empty($_POST['category']) || !empty($_POST['year'])) {
$sql = "SELECT * FROM notices WHERE ";
$conditions = array();
// Check each provided criterion and add it to the SQL query
if (!empty($_POST['title'])) {
$conditions[] = "TITLE LIKE '%" . $_POST['title'] . "%'";
}
if (!empty($_POST['date'])) {
$conditions[] = "DATE = '" . $_POST['date'] . "'";
}
if (!empty($_POST['upload_till'])) {
$conditions[] = "`UPLOAD TILL` = '" . $_POST['upload_till'] . "'";
}
if (!empty($_POST['category'])) {
$conditions[] = "CATEGORY = '" . $_POST['category'] . "'";
}
if (!empty($_POST['year'])) {
$conditions[] = "YEAR = '" . $_POST['year'] . "'";
}
// Combine all conditions using "AND" operator
$sql .= implode(" AND ", $conditions);
// Execute the SQL query
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$rows = array();
// Fetch all matching rows
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
// Send the JSON response containing all matching rows
echo json_encode($rows);
} else {
echo "No notice found matching the search criteria.";
}
} else {
echo "Please provide at least one search criterion.";
}
} else {
echo "Invalid request method.";
}
$conn->close();
?>