-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
56 lines (51 loc) · 1.76 KB
/
search.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
<?php
// Database connection
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'shop';
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Failed to connect to the database: " . mysqli_connect_error());
}
if (isset($_GET['query'])) {
$search_query = $_GET['query'];
// SQL query to search for products using the name
$sql = "SELECT * FROM products WHERE name LIKE '%$search_query%'";
$result = mysqli_query($conn, $sql);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search Results</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h1 class="text-center">Search Results</h1>
<?php
if (isset($result) && mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<div class="row">';
echo '<div class="col-md-6 offset-md-3">';
echo '<div class="product-card text-center">';
echo '<img src="' . $row['image_path'] . '" alt="' . $row['name'] . '" class="img-fluid">';
echo '<h2>' . $row['name'] . '</h2>';
echo '<p class="mb-3">Price: ' . $row['price'] . ' SAR</p>';
echo '<a href="product.php?id=' . $row['id'] . '" class="btn btn-primary">View Details</a>';
echo '</div>';
echo '</div>';
echo '</div>';
}
} else {
echo '<p class="text-center">No results found for: ' . $search_query . '</p>';
}
?>
</div>
</body>
</html>
<?php
// Close the database connection
mysqli_close($conn);
?>