Advertisement
dimipan80

Computer Shop

May 1st, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. <?php
  2. $productList = preg_split("/\r?\n/", $_GET['list'], -1, PREG_SPLIT_NO_EMPTY);
  3. $minPrice = (float)$_GET['minPrice'];
  4. $maxPrice = (float)$_GET['maxPrice'];
  5. $filter = $_GET['filter'];
  6. $order = $_GET['order'];
  7.  
  8. $idCounter = 0;
  9. $filteredList = array();
  10. foreach ($productList as $productInfo) {
  11.     $idCounter++;
  12.     $productInfo = preg_split('/\s*\|\s*/', $productInfo, -1, PREG_SPLIT_NO_EMPTY);
  13.     $price = (float)$productInfo[3];
  14.     if ($price < $minPrice || $price > $maxPrice || ($filter != 'all' && $filter != $productInfo[1])) {
  15.         continue;
  16.     }
  17.  
  18.     $id = $idCounter;
  19.     $name = htmlspecialchars($productInfo[0]);
  20.     $components = preg_split('/\s*\,\s*/', $productInfo[2], -1, PREG_SPLIT_NO_EMPTY);
  21.     $filteredList[] = array('id' => $id, 'name' => $name, 'components' => $components, 'price' => $price);
  22. }
  23.  
  24. usort($filteredList, function ($a, $b) use ($order) {
  25.     if ($a['price'] === $b['price']) {
  26.         return ($a['id'] > $b['id']) ? 1 : -1;
  27.     }
  28.  
  29.     if ($order == 'ascending') {
  30.         return ($a['price'] > $b['price']) ? 1 : -1;
  31.     }
  32.  
  33.     return $a['price'] < $b['price'] ? 1 : -1;
  34. });
  35.  
  36. foreach ($filteredList as $productInfo) {
  37.     echo "<div class=\"product\" id=\"product{$productInfo['id']}\"><h2>{$productInfo['name']}</h2><ul>";
  38.     foreach ($productInfo['components'] as $component) {
  39.         echo '<li class="component">' . htmlspecialchars($component) . '</li>';
  40.     }
  41.  
  42.     echo '</ul><span class="price">' . number_format($productInfo['price'], 2, '.', '') . '</span></div>';
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement