Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $list = explode("\n", $_GET['list']);
- $minPrice = $_GET['minPrice'];
- $maxPrice = $_GET['maxPrice'];
- $filter = $_GET['filter'];
- $order = $_GET['order'];
- $id = 1;
- $productsInfo = [];
- $products = [];
- foreach ($list as $line) {
- if (strlen($line) < 2) {
- continue;
- } else {
- $productsInfo = explode("|", trim($line));
- if ($filter == "all" || $filter == trim($productsInfo[1])) {
- if (floatval(trim($productsInfo[3])) >= floatval($minPrice) && floatval(trim($productsInfo[3])) <= floatval($maxPrice)) {
- $products[] = [
- 'id' => $id,
- 'name' => trim($productsInfo[0]),
- 'type' => trim($productsInfo[1]),
- 'components' => explode(', ', trim($productsInfo[2])),
- 'price' => floatval(trim($productsInfo[3]))
- ];
- }
- }
- $id++;
- }
- }
- usort($products, function($a, $b) use ($order) {
- if ($a['price'] == $b['price']) {
- return $a['id'] < $b['id'] ? -1 : 1;
- }
- return ($order == "ascending" ^ $a['price'] < $b['price']) ? 1 : -1;
- });
- $output = "";
- for ($i = 0; $i < count($products); $i++) {
- $price = htmlspecialchars($products[$i]['price']);
- $priceFormatted = number_format(round($price, 2), 2 , '.' , '');
- $output .= "<div class=\"product\" id=\"product" . $products[$i]['id'] . "\">";
- $output .= "<h2>" . htmlspecialchars($products[$i]['name']) ."</h2>";
- $output .= "<ul>";
- for ($j = 0; $j < count($products[$i]['components']); $j++) {
- $output .= "<li class=\"component\">" . htmlspecialchars($products[$i]['components'][$j]) . "</li>";
- }
- $output .= "</ul>";
- $output .= "<span class=\"price\">" . $priceFormatted ."</span>";
- $output .= "</div>";
- }
- echo $output;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement