Advertisement
Guest User

04.ComputerShop

a guest
Sep 6th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. <?php
  2. $list = explode("\n", $_GET['list']);
  3. $minPrice = $_GET['minPrice'];
  4. $maxPrice = $_GET['maxPrice'];
  5. $filter = $_GET['filter'];
  6. $order = $_GET['order'];
  7.  
  8. $id = 1;
  9. $productsInfo = [];
  10. $products = [];
  11. foreach ($list as $line) {
  12.     if (strlen($line) < 2) {
  13.         continue;
  14.     } else {
  15.         $productsInfo = explode("|", trim($line));
  16.         if ($filter == "all" || $filter == trim($productsInfo[1])) {
  17.             if (floatval(trim($productsInfo[3])) >= floatval($minPrice) && floatval(trim($productsInfo[3])) <= floatval($maxPrice)) {
  18.                 $products[] = [
  19.                     'id' => $id,
  20.                     'name' => trim($productsInfo[0]),
  21.                     'type' => trim($productsInfo[1]),
  22.                     'components' => explode(', ', trim($productsInfo[2])),
  23.                     'price' => floatval(trim($productsInfo[3]))
  24.                 ];
  25.             }
  26.         }
  27.         $id++;
  28.     }
  29. }
  30.  
  31. usort($products, function($a, $b) use ($order) {
  32.     if ($a['price'] == $b['price']) {
  33.     return $a['id'] < $b['id'] ? -1 : 1;
  34. }
  35.     return ($order == "ascending" ^ $a['price'] < $b['price']) ? 1 : -1;
  36. });
  37.  
  38. $output = "";
  39. for ($i = 0; $i < count($products); $i++) {
  40.     $price = htmlspecialchars($products[$i]['price']);
  41.     $priceFormatted = number_format(round($price, 2), 2 , '.' , '');
  42.     $output .= "<div class=\"product\" id=\"product" . $products[$i]['id'] . "\">";
  43.     $output .= "<h2>" . htmlspecialchars($products[$i]['name']) ."</h2>";
  44.     $output .= "<ul>";
  45.     for ($j = 0; $j < count($products[$i]['components']); $j++) {
  46.         $output .= "<li class=\"component\">" . htmlspecialchars($products[$i]['components'][$j]) . "</li>";
  47.     }
  48.     $output .= "</ul>";
  49.     $output .= "<span class=\"price\">" . $priceFormatted ."</span>";
  50.     $output .= "</div>";
  51. }
  52.  
  53. echo $output;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement