-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.php
More file actions
75 lines (59 loc) · 2.57 KB
/
search.php
File metadata and controls
75 lines (59 loc) · 2.57 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
// Include your database connection file
include 'db_connection.php';
// Initialize variables to hold the category and search term
$selectedCategory = "";
$searchKeywords = ""; // Use the correct variable name
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the selected category and search term from the form
$selectedCategory = $_POST['category'];
$searchKeywords = $_POST['search_keywords']; // Use the correct name
}
// Prepare the SQL query with placeholders for category and multi-keyword search
$sql = "SELECT a.*, d.nama_departement
FROM asset a
LEFT JOIN tbldepartments d ON a.id_departement = d.id_departement
WHERE a.id_category = :id_category";
// Check if a search term is provided
if (!empty($searchKeywords)) {
$sql .= " AND (a.nama_asset LIKE :searchTerm OR a.used_by LIKE :searchTerm OR d.nama_departement LIKE :searchTerm)";
}
// Prepare the SQL query
$stmt = $db->prepare($sql);
// Bind the category parameter
$stmt->bindParam(':id_category', $selectedCategory);
// Bind the search term parameter if applicable
if (!empty($searchKeywords)) {
$searchPattern = "%{$searchKeywords}%";
$stmt->bindValue(":searchTerm", $searchPattern, PDO::PARAM_STR);
}
// Execute the query
$stmt->execute();
// Fetch the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Display the results
$totalAssetsByDepartment = array(); // Initialize an array to store total assets per department
foreach ($results as $result) {
// Output the data as needed
echo "Asset Name: " . $result['nama_asset'] . "<br>";
echo "Kode asset: " . $result['code_asset'] . "<br>";
echo "Department: " . $result['nama_departement'] . "<br>"; // Display department name
// Add more fields as needed
echo "<hr>";
// Increment the total assets for the department
$departmentName = $result['nama_departement'];
if (!isset($totalAssetsByDepartment[$departmentName])) {
$totalAssetsByDepartment[$departmentName] = 1;
} else {
$totalAssetsByDepartment[$departmentName]++;
}
}
// Display the total number of assets for each department
foreach ($totalAssetsByDepartment as $department => $totalAssets) {
echo "$department: " . $totalAssets . "<br>";
}
// Display the total number of assets for all departments
$overallTotalAssets = count($results);
echo "Overall Total Assets: " . $overallTotalAssets;
?>