-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.php
More file actions
164 lines (137 loc) · 5.45 KB
/
Copy pathsearch.php
File metadata and controls
164 lines (137 loc) · 5.45 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
require_once "conn.php";
require_once "nav.php";
$searchQuery = isset($_GET['search']) ? $_GET['search'] : '';
$sort_by = isset($_GET['sort_by']) ? $_GET['sort_by'] : 'PID ASC';
function searchProducts($conn, $searchQuery)
{
$searchQuery = trim($searchQuery);
$searchQuery = filter_var($searchQuery, FILTER_SANITIZE_STRING);
$searchQuery = mysqli_real_escape_string($conn, $searchQuery);
if (!empty($searchQuery)) {
$queryParts = [];
// Search for substrings in product name
$queryParts[] = "product_name LIKE '%{$searchQuery}%'";
// Search for substrings in category
$queryParts[] = "category LIKE '%{$searchQuery}%'";
// Search for substrings in brand
$queryParts[] = "brand LIKE '%{$searchQuery}%'";
$query = implode(' OR ', $queryParts);
$stmt = $conn->prepare("SELECT * FROM product WHERE {$query}");
return $stmt;
}
return null;
}
// function getSortedProducts($conn, $sort_by)
// {
// $sql = "SELECT * FROM product ORDER BY ";
// switch ($sort_by) {
// case 'price_asc':
// $sql .= "MRP ASC";
// break;
// case 'price_desc':
// $sql .= "MRP DESC";
// break;
// case 'discount_asc':
// $sql .= "discount ASC";
// break;
// case 'discount_desc':
// $sql .= "discount DESC";
// break;
// default:
// $sql .= "PID ASC";
// }
// $result = mysqli_query($conn, $sql);
// return $result;
// }
$searchResult = searchProducts($conn, $searchQuery);
if ($searchResult) {
$searchResult->execute();
// echo "<pre>",var_dump($searchResult);
$result = $searchResult->get_result();
// $sort_by = isset($_GET['sort_by']) ? $_GET['sort_by'] : 'PID ASC';
} else {
// $result = getSortedProducts($conn, $sort_by);
}
if (mysqli_num_rows($result) > 0) {
echo '<div class="container"><br><BR>';
echo '<div class="row product">';
$count = 0;
while ($row = $result->fetch_assoc()) {
if ($count % 3 == 0 && $count != 0) {
echo '</div><div class="row product">';
}
$pid = $row['PID'];
$stmt1 = $conn->prepare("SELECT img_address FROM images WHERE PID =$pid");
$stmt1->execute();
$imgres = $stmt1->get_result();
$banner_carousel = 0;
$message = '';
$selling_price = (int) $row['MRP'] - $row['MRP'] / 100 * $row['discount'];
echo '<div class="col-md-4">';
echo '<a href="product_detail.php?pid=' . $pid . '" style="text-decoration: none; color: inherit;">';
echo '<div class="card" style="border-radius: 15px;">';
echo '<div class="card-body">';
echo '<div class="row">';
echo '<div class="col-sm-12">';
echo '<div class="carousel slide" data-interval="5000" data-ride="carousel">';
echo '<div class="carousel-inner">';
// Display the images
if ($imgres->num_rows > 0) {
while ($_row = $imgres->fetch_assoc()) {
$imagePath = $_row['img_address'];
if (file_exists($imagePath)) {
if ($banner_carousel === 0) {
echo "<div class='carousel-item active'>";
echo '<img src="' . $imagePath . '" class=img-fluid alt="Product Image">';
echo "</div>";
$banner_carousel++;
} else {
echo "<div class='carousel-item'>";
echo '<img src="' . $imagePath . '" class=img-fluid alt="Product Image">';
echo "</div>";
}
} else {
$message++;
}
}
}
if (!$message == 0) {
echo "<p class='text-center text-danger'>Error occured while loading images!</p>";
}
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="card-footer">';
echo '<h5 class="card-title"></h5>';
echo '<p class="card-text"><strong>', $row["product_name"], '</strong></p>';
echo '<p class="card-text"><strong>MRP:</strong><strike>', '₹', $row["MRP"], '</strike></p>';
echo '<p class="card-text"><strong>Discount:</strong><span class="text-success">', $row["discount"], '% off</span></p>';
echo '<p class="card-text"><strong>Selling Price: </strong>', $selling_price, '</p>';
// echo '<button class="btn btn-primary btn-block" >Add to Cart</button>';
// echo '<button class="btn btn-warning btn-block" onclick="buyNow(' . $pid . ')">Buy Now</button>';
echo '</div>';
echo '</div></a>';
echo '</div>';
$count++;
}
} else {
echo '<center> <div class="alert alert-dark w-50 mx-auto d-block" style="top:300px;">
<strong> No Products Found </strong><br><br><br>
<a href="index.php"><button class="btn btn-warning">Go Home</button></a>
</div>';
}
mysqli_close($conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="displayproduct.css">
</head>
<body>
</body>
</html>