-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_search_results.php
121 lines (109 loc) · 4.73 KB
/
get_search_results.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
<?php
//initialise database
require __DIR__ . '/vendor/autoload.php';
$mongoClient = (new MongoDB\Client);
$db = $mongoClient->ecommerce;
$allProducts = $db->products;
//get values from input send from client
$words = filter_input(INPUT_POST, 'words', FILTER_SANITIZE_STRING);
$type = filter_input(INPUT_POST, 'type', FILTER_SANITIZE_STRING);
//create arrays to keep values and be able to sort them later
$arrUnsorted = [];
$alreadyFound = [];
//only display top 10 found items.
$maxValue = 50;
$currAmount = 0;
//need to have 2 cursors if not ide complains about multiple iterations on 1 cursor
$allall = $db->products->find();
$allall2 = $db->products->find();
//if nothing is sent from search or there are no recommended items, then just get first 10 items found
if ($words == ',,') {
foreach ($allall as $m) {
if ($currAmount < $maxValue) {
array_push($arrUnsorted, $m);
}
$currAmount++;
}
} else {
//otherwise get either recommended or searched words, both are stored in $words here
$wordList = explode(",", $words);
foreach ($allall2 as $m) {
//for each word go through the wordlist and see if either the name or the description contains the searched word
for ($i = 0; $i < sizeof($wordList); $i++) {
//the recommended can pass in null words, so we need to check that this word is not null
if ($wordList[$i] != '') {
//check if word has already been added the found list, otherwise add it to unsorted and alreadyfound
$exists = 0;
for ($k = 0; $k < count($alreadyFound); $k++) {
if ($m['id'] === $alreadyFound[$k]) {
$exists = 1;
}
}
if ($exists == 0) {
//the 2 ifs check for inclusion in the desc and name
if (strpos($m['name'], $wordList[$i]) !== false) {
array_push($arrUnsorted, $m);
array_push($alreadyFound, $m['id']);
$currAmount++;
} else if (strpos($m['description'], $wordList[$i]) !== false) {
array_push($arrUnsorted, $m);
array_push($alreadyFound, $m['id']);
$currAmount++;
}
}
}
}
}
}
//type 2 sorts from low to hight
if ($type == 2) {
for ($i = 0; $i < sizeof($arrUnsorted); $i++) {
$minimum = $i;
for ($k = $i + 1; $k < sizeof($arrUnsorted); $k++) {
if ($arrUnsorted[$k]['cost'] < $arrUnsorted[$minimum]['cost']) {
$minimum = $k;
}
}
$temp = $arrUnsorted[$i];
$arrUnsorted[$i] = $arrUnsorted[$minimum];
$arrUnsorted[$minimum] = $temp;
}
//type 3 sorts from high to low
} else if ($type == 3) {
for ($i = 0; $i < sizeof($arrUnsorted); $i++) {
$minimum = $i;
for ($k = $i + 1; $k < sizeof($arrUnsorted); $k++) {
if ($arrUnsorted[$k]['cost'] > $arrUnsorted[$minimum]['cost']) {
$minimum = $k;
}
}
$temp = $arrUnsorted[$i];
$arrUnsorted[$i] = $arrUnsorted[$minimum];
$arrUnsorted[$minimum] = $temp;
}
}
//add all ids to string to be sent back and parsed on client side.
for ($i = 0; $i < sizeof($arrUnsorted); $i++) {
echo $arrUnsorted[$i]['id'] . ',';
}
//need to know when to split the string for doing displaying, putting in list
echo '-------';
for ($i = 0; $i < sizeof($arrUnsorted); $i++) {
//creating a div with all info and button to add to cart
echo '<div>';
echo "<img src=\"uploadedImages/" . $arrUnsorted[$i]['img'] . "\" alt=\"\">";
echo '<h3>' . $arrUnsorted[$i]['name'] . '</h3>';
echo '<p>' . $arrUnsorted[$i]['description'] . '</p>';
echo '<h2>$' . $arrUnsorted[$i]['cost'] . '</h2>';
echo '<p>Available Quantity: ' . $arrUnsorted[$i]['stkCount'] . '</p>';
echo '<button onclick="addToCart(q' . $i . ')">Add to cart</button>';
echo '<label for="quantity">Qty</label>
<select id="q' . $i . '">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>';
echo '</div>';
}
?>