-
Notifications
You must be signed in to change notification settings - Fork 1
/
sort-items.php
59 lines (48 loc) · 2.09 KB
/
sort-items.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
<?php
//Include libraries
require __DIR__ . '/vendor/autoload.php';
//Create instance of MongoDB client
$mongoClient = (new MongoDB\Client);
//Select a database
$db = $mongoClient->perfumefest;
// getting value of selectd sorted option
$sortOpt = filter_input(INPUT_POST, 'sortBy', FILTER_SANITIZE_STRING);
// checking if sort option is price from low to high
if ($sortOpt == "PriceAsc") {
$filter = [];
$options = ['sort' => ['Price' => 1]];
$cursor = $mongoClient->$db->Products->find($filter, $options);
// checking if sort option is price from high to low
} else if ($sortOpt == "PriceDec") {
$filter = [];
$options = ['sort' => ['Price' => -1]];
$cursor = $mongoClient->$db->Products->find($filter, $options);
// checking if sort option is in alphabatical order
} else if ($sortOpt == "AtoZ") {
$filter = [];
$options = ['sort' => ['Name' => 1]];
$cursor = $mongoClient->$db->Products->find($filter, $options);
// checking if no option was selected
} else {
foreach ($cursor as $product) {
echo '<div class="perfume-data">';
echo ' <h1 class="item-msg"></h1>';
echo ' <div class="item-picture"><img class="item-img" src="./' . $product['img_url'] . '" alt=""></div>';
echo ' <div class="item-name">' . $product['Name'] . '</div>';
echo ' <div class="item-size">' . $product['size'] . '</div>';
echo ' <div class="item-price">£' . $product['Price'] . '</div>';
echo ' <button class="addbtn" >Add to Cart</button>
</div>';
}
}
// echoing back sorted results depending on posted value
foreach ($cursor as $product) {
echo '<div class="perfume-data">';
echo ' <h1 class="item-msg"></h1>';
echo ' <div class="item-picture"><img class="item-img" src="./' . $product['img_url'] . '" alt=""></div>';
echo ' <div class="item-name">' . $product['Name'] . '</div>';
echo ' <div class="item-size">' . $product['size'] . '</div>';
echo ' <div class="item-price">£' . $product['Price'] . '</div>';
echo ' <button class="addbtn" >Add to Cart</button>
</div>';
}