-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshop_all.php
266 lines (252 loc) · 10.4 KB
/
shop_all.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
session_start();
include('config/dbcon.php');
include('components/header.php');
include('components/navbar.php');
include('components/frontbar.php');
?>
<main class="shop_all_page">
<?php
if(isset($_POST['search_btnn']) && $_POST['search_box'] != ''){
$search_box = mysqli_real_escape_string($connection, $_POST['search_box']);
?>
<div>
<h1>Searched for "<?= $search_box;?>"</h1>
</div>
<div class="shop_all_container">
<div class="shop_category">
<h3 class="category_title" onclick="showItems();">
Category
<span class="js-arrow-5"><img class="arrow" src="https://img.icons8.com/forma-thin-filled/24/1A1A1A/play.png" alt="play"/></span>
</h3>
<div class="shop_category_container">
<?php
$sql = "SELECT categories.id, categories.name, categories.status, COUNT(products.id) as product_count
FROM categories
LEFT JOIN products ON categories.id = products.category_id
GROUP BY categories.id, categories.name";
$result = mysqli_query($connection, $sql);
if ($result) {
if (mysqli_num_rows($result) > 0) {
foreach ($result as $items) {
if ($items["status"] != "Hidden") {
?>
<div class="checkbox-container">
<input type="checkbox" name="categories[]" value="<?= $items["id"]; ?>" id="category_<?= $items["id"]; ?>" class="styled-checkbox">
<label for="category_<?= $items["id"]; ?>"><?= $items["name"]; ?> (<?= $items["product_count"]; ?>)</label>
</div>
<?php
}
}
?>
<a href="shop_all.php" ><span>All Products</span></a>
<?php
} else {
?>
<div class="each_category">
<p>No Products Available!</p>
</div>
<?php
}
} else {
?>
<div class="each_category">
<p>Execution Error: <?= $connection->error; ?></p>
</div>
<?php
}
?>
</div>
<a href="prodcuts.php?category=Computers" ><h3>Browser</h3></a>
</div>
<!-- result box used start-->
<?php
// Define the number of products per page
$products_per_page = 10;
// Get the current page number from the URL, default to 1 if not set
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$page = max($page, 1); // Ensure the page number is at least 1
// Calculate the offset for the SQL query
$offset = ($page - 1) * $products_per_page;
// Connect to the database (make sure you have $connection already defined)
// Get the total number of products
$total_products_query = "SELECT COUNT(*) AS total FROM products";
$total_products_result = mysqli_query($connection, $total_products_query);
$total_products = mysqli_fetch_assoc($total_products_result)['total'];
// Calculate the total number of pages
$total_pages = ceil($total_products / $products_per_page);
// Fetch products for the current page
$sql = "SELECT * FROM products WHERE product_name LIKE '%{$search_box}%' LIMIT $products_per_page OFFSET $offset";
$result = mysqli_query($connection, $sql);
$sql2 = "SELECT id FROM categories WHERE name LIKE '%{$search_box}%' LIMIT $products_per_page OFFSET $offset";
$result2 = mysqli_query($connection, $sql2);
?>
<div class="shop_products">
<div class="product-containerr" id="product-container">
<?php
if ($result || $result2) {
if (mysqli_num_rows($result2) > 0) {
// -- result box used start {search category} --
$category = mysqli_fetch_assoc($result2);
$category_id = $category["id"];
$sql3 = "SELECT * FROM products WHERE category_id = '$category_id' LIMIT $products_per_page OFFSET $offset";
$result3 = mysqli_query($connection, $sql3);
if($result3){
foreach ($result3 as $items) {
include 'functions/product_template.php'; // -- 1
}
}else {
?>
<div class="each_category">
<p>No Products Found!</p>
</div>
<?php
}
unset($_POST['search_btnn']);
unset($_POST['search_box']);
}else if (mysqli_num_rows($result) > 0) {
// -- result box used start {search prodcuts} --
foreach ($result as $items) {
include 'functions/product_template.php'; // -- 2
}
unset($_POST['search_btnn']);
unset($_POST['search_box']);
} else {
?>
<div class="each_category">
<p>No Products Found!</p>
</div>
<?php
}
} else {
?>
<div class="each_category">
<p>Something Went Wrong! Sorry About that.</p>
</div>
<?php
}
?>
</div>
</div>
<?php
unset($_POST['search_btnn']);
unset($_POST['search_box']);
?>
<!-- result box used end-->
</div>
<?php
}else{
?>
<!-- DID NOT use the search box start-->
<div>
<h1>Search Results</h1>
</div>
<div class="shop_all_container">
<div class="shop_category">
<h3 class="category_title" onclick="showItems();">
Category
<span class="js-arrow-5"><img class="arrow" src="https://img.icons8.com/forma-thin-filled/24/1A1A1A/play.png" alt="play"/></span>
</h3>
<div class="shop_category_container">
<?php
$sql = "SELECT categories.id, categories.name, categories.status, COUNT(products.id) as product_count
FROM categories
LEFT JOIN products ON categories.id = products.category_id
GROUP BY categories.id, categories.name";
$result = mysqli_query($connection, $sql);
if ($result) {
if (mysqli_num_rows($result) > 0) {
foreach ($result as $items) {
if ($items["status"] != "Hidden") {
?>
<div class="checkbox-container">
<input type="checkbox" name="categories[]" value="<?= $items["id"]; ?>" id="category_<?= $items["id"]; ?>" class="styled-checkbox">
<label for="category_<?= $items["id"]; ?>"><?= $items["name"]; ?> (<?= $items["product_count"]; ?>)</label>
</div>
<?php
}
}
} else {
?>
<div class="each_category">
<p>No Products Available!</p>
</div>
<?php
}
} else {
?>
<div class="each_category">
<p>Execution Error: <?= $connection->error; ?></p>
</div>
<?php
}
?>
</div>
<a href="prodcuts.php?category=Computers" ><h3>Browser</h3></a>
</div>
<?php
// Define the number of products per page
$products_per_page = 10;
// Get the current page number from the URL, default to 1 if not set
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$page = max($page, 1); // Ensure the page number is at least 1
// Calculate the offset for the SQL query
$offset = ($page - 1) * $products_per_page;
// Connect to the database (make sure you have $connection already defined)
// Get the total number of products
$total_products_query = "SELECT COUNT(*) AS total FROM products";
$total_products_result = mysqli_query($connection, $total_products_query);
$total_products = mysqli_fetch_assoc($total_products_result)['total'];
// Calculate the total number of pages
$total_pages = ceil($total_products / $products_per_page);
// Fetch products for the current page
$sql = "SELECT * FROM products LIMIT $products_per_page OFFSET $offset";
$result = mysqli_query($connection, $sql);
?>
<div class="shop_products">
<div class="product-containerr" id="product-container">
<?php
if ($result) {
if (mysqli_num_rows($result) > 0) {
foreach ($result as $items) {
include 'functions/product_template.php'; // -- 3
}
} else {
?>
<div class="each_category">
<p>No Products Available!</p>
</div>
<?php
}
} else {
?>
<div class="each_category">
<p>Something Went Wrong! Sorry About that.</p>
</div>
<?php
}
?>
</div>
<div class="pagination">
<?php if ($page > 1): ?>
<a href="?page=<?= $page - 1 ?>" id="prev-button">< previous</a>
<?php else: ?>
<span class="disabled_button" id="prev-button" disabled>< previous</span>
<?php endif; ?>
<span id="">Page <?= $page ?> of <?= $total_pages ?></span>
<?php if ($page < $total_pages): ?>
<a href="?page=<?= $page + 1 ?>" id="next-button">next ></a>
<?php else: ?>
<span class="disabled_button" id="next-button" disabled>next ></span>
<?php endif; ?>
</div>
</div>
<!-- DID NOT use the search box end-->
</div>
<?php
}
?>
</main>
<?php
include('components/footer.php');
?>