Advertisement
Aleksiev

PHP Exam - Computer Shop

Sep 5th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. <?php
  2.  
  3. $list = trim($_GET['list']);
  4. $minPrice = (double) trim($_GET['minPrice']);
  5. $maxPrice = (double) trim($_GET['maxPrice']);
  6. $filter = trim($_GET['filter']);
  7. $order = trim($_GET['order']);
  8. $result = '';
  9.  
  10. if ($order == 'ascending') {
  11.     $sort = SORT_ASC;
  12. } else if ($order == 'descending') {
  13.     $sort = SORT_DESC;
  14. }
  15.  
  16. $prds = preg_split('/\n/', $list);
  17.  
  18. if (sizeof($prds) > 1 && $minPrice < $maxPrice) {
  19.     $productsListList = array();
  20.     $products = array();
  21.  
  22.     foreach ($prds as $key => $p) {
  23.         $product = explode(' | ', $p);
  24.  
  25.         $productsList[$key]['id'] = $key + 1;
  26.         $productsList[$key]['name'] = $product[0];
  27.         $productsList[$key]['type'] = $product[1];
  28.         $productsList[$key]['components'] = explode(', ', $product[2]);
  29.         $productsList[$key]['price'] = (double) $product[3];
  30.     }
  31.  
  32.     foreach ($productsList as $p) {
  33.         if ($p['price'] >= $minPrice && $p['price'] <= $maxPrice && ($filter == $p['type'] || $filter == 'all')) {
  34.             $cnt = sizeof($products);
  35.             $products[$cnt] = $p;
  36.         }
  37.     }
  38.  
  39.     $products = multisort($products, SORT_ASC, 'id');
  40.     $products = multisort($products, $sort, 'price');
  41.  
  42.     foreach ($products as $p) {
  43.         $result .= '<div class="product" id="product' . $p['id'] . '"><h2>' . htmlspecialchars($p['name']) . '</h2><ul>';
  44.  
  45.         foreach ($p['components'] as $c) {
  46.             $result .= '<li class="component">' . htmlspecialchars($c) . '</li>';
  47.         }
  48.  
  49.         $result .= '</ul><span class="price">' . number_format($p['price'], 2, '.', '') . '</span></div>';
  50.     }
  51.  
  52.     echo $result;
  53. }
  54.  
  55. function multisort($arr, $ord, $property) {
  56.     foreach ($arr as $id => $val) {
  57.         $key[$id] = $val[$property];
  58.     }
  59.     array_multisort($key, $ord, $arr);
  60.     return $arr;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement