Browse Source

Add online store search ( issue #8 )

master
Skylar Ittner 1 year ago
parent
commit
d180061bf8
3 changed files with 56 additions and 2 deletions
  1. 3
    0
      public/index.php
  2. 7
    2
      public/parts/nav.php
  3. 46
    0
      public/parts/search.php

+ 3
- 0
public/index.php View File

@@ -32,6 +32,9 @@ if (isset($_GET['page'])) {
32 32
                 $page = "404";
33 33
             }
34 34
             break;
35
+        case "search":
36
+            $page = "search";
37
+            break;
35 38
         case "item":
36 39
             if (isset($_GET['id']) && $binstack->has('items', ['itemid' => $_GET['id']])) {
37 40
                 $page = "item";

+ 7
- 2
public/parts/nav.php View File

@@ -36,9 +36,14 @@ foreach ($cats as $c) {
36 36
                 </div>
37 37
             </li>
38 38
         </ul>
39
-        <form class="form-inline ml-3">
39
+        <form class="form-inline ml-3" method="GET">
40 40
             <div class="input-group">
41
-                <input class="form-control" type="search" placeholder="Search" aria-label="Search">
41
+                <input type="hidden" name="page" value="search" />
42
+                <input name="q" class="form-control" type="search" placeholder="Search" aria-label="Search" value="<?php
43
+                if (isset($_GET['q'])) {
44
+                    echo htmlspecialchars($_GET['q']);
45
+                }
46
+                ?>">
42 47
                 <div class="input-group-addon">
43 48
                     <button class="btn btn-success" type="submit"><i class="fas fa-search"></i> <span class="d-none d-md-inline">Search</span></button>
44 49
                 </div>

+ 46
- 0
public/parts/search.php View File

@@ -0,0 +1,46 @@
1
+<?php
2
+/*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
+ */
7
+
8
+if (!defined('NICKELBOX')) {
9
+    die("Direct access denied.");
10
+}
11
+$query = $_GET['q'];
12
+
13
+$dbitems = $binstack->select('items', 'itemid', [
14
+    'AND' => [
15
+        'price[>]' => 0,
16
+        'OR' => [
17
+            'name[~]' => $query,
18
+            'code1' => $query,
19
+            'code2' => $query,
20
+            'text1[~]' => $query
21
+        ]
22
+    ], 'LIMIT' => 20]);
23
+
24
+$items = [];
25
+
26
+foreach ($dbitems as $i) {
27
+    $items[] = new Item($i);
28
+}
29
+?>
30
+<div class="container mt-4">
31
+    <h1 class="display-4">Search <span class="h3 ml-3 d-none"><?php echo htmlspecialchars($query); ?></span></h1>
32
+
33
+    <div class="list-group list-group-flush">
34
+        <?php
35
+        if (count($items) > 0) {
36
+            foreach ($items as $i) {
37
+                echo RenderItem::line($i);
38
+            }
39
+        } else {
40
+            ?>
41
+            <p>No results found for <?php echo htmlspecialchars($query); ?>.</p>
42
+            <?php
43
+        }
44
+        ?>
45
+    </div>
46
+</div>

Loading…
Cancel
Save