Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $productList = preg_split("/\r?\n/", $_GET['list'], -1, PREG_SPLIT_NO_EMPTY);
- $minPrice = (float)$_GET['minPrice'];
- $maxPrice = (float)$_GET['maxPrice'];
- $filter = $_GET['filter'];
- $order = $_GET['order'];
- $idCounter = 0;
- $filteredList = array();
- foreach ($productList as $productInfo) {
- $idCounter++;
- $productInfo = preg_split('/\s*\|\s*/', $productInfo, -1, PREG_SPLIT_NO_EMPTY);
- $price = (float)$productInfo[3];
- if ($price < $minPrice || $price > $maxPrice || ($filter != 'all' && $filter != $productInfo[1])) {
- continue;
- }
- $id = $idCounter;
- $name = htmlspecialchars($productInfo[0]);
- $components = preg_split('/\s*\,\s*/', $productInfo[2], -1, PREG_SPLIT_NO_EMPTY);
- $filteredList[] = array('id' => $id, 'name' => $name, 'components' => $components, 'price' => $price);
- }
- usort($filteredList, function ($a, $b) use ($order) {
- if ($a['price'] === $b['price']) {
- return ($a['id'] > $b['id']) ? 1 : -1;
- }
- if ($order == 'ascending') {
- return ($a['price'] > $b['price']) ? 1 : -1;
- }
- return $a['price'] < $b['price'] ? 1 : -1;
- });
- foreach ($filteredList as $productInfo) {
- echo "<div class=\"product\" id=\"product{$productInfo['id']}\"><h2>{$productInfo['name']}</h2><ul>";
- foreach ($productInfo['components'] as $component) {
- echo '<li class="component">' . htmlspecialchars($component) . '</li>';
- }
- echo '</ul><span class="price">' . number_format($productInfo['price'], 2, '.', '') . '</span></div>';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement