-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
81 lines (63 loc) · 1.96 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
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
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['squery']) )
{
$squery = $_POST['squery'];
$squery = '"'.$squery.'%"';
$machine = "localhost";
$user = "root";
$password = "";
$db_name = "main";
$connection = mysqli_connect($machine , $user , $password , $db_name);
if (!$connection)
echo "Could not connect";
$main_query = "select * from products where name like $squery;";
$result = mysqli_query($connection , $main_query);
$extensions = ['webp', 'jpeg', 'jpg', 'avif' , 'png'];
$search_result = [];
if (mysqli_num_rows($result) > 0)
{
while ($current_row = mysqli_fetch_assoc($result))
{
$imagePath = null;
foreach ($extensions as $ext)
{
if (file_exists("assets/images/{$current_row['id']}.$ext"))
{
$imagePath = "assets/images/{$current_row['id']}.$ext";
break;
}
}
$search_result[] = [
'name' => $current_row['name'],
'category' => $current_row['category'],
'id' => $current_row['id'],
'img' => $imagePath
];
}
}
$main_query = "select * from products where category like $squery;";
$result = mysqli_query($connection , $main_query);
if (mysqli_num_rows($result) > 0)
{
while ($current_row = mysqli_fetch_assoc($result))
{
$imagePath = null;
foreach ($extensions as $ext)
{
if (file_exists("assets/images/{$current_row['id']}.$ext"))
{
$imagePath = "assets/images/{$current_row['id']}.$ext";
break;
}
}
$search_result[] = [
'name' => $current_row['name'],
'category' => $current_row['category'],
'id' => $current_row['id'],
'img' => $imagePath
];
}
}
echo json_encode($search_result);
}
?>