Advertisement
Valleri

Computer Store

Sep 5th, 2014
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. <?php
  2. $text = $_GET['list'];
  3. $minPrice = $_GET['minPrice'];
  4. $maxPrice = $_GET['maxPrice'];
  5. $filter = $_GET['filter'];
  6. $order = $_GET['order'];
  7.  
  8.  
  9.  
  10. $property = "price";
  11. $arrayOfProducts = array();
  12.  
  13. $products = preg_split("/\r?\n/", $text, -1, PREG_SPLIT_NO_EMPTY);
  14.  
  15.  
  16. $counter = 1;
  17. array_walk($products, function(&$val) {
  18.     $val .= "|" . $GLOBALS['counter'];
  19.     $GLOBALS['counter']++;
  20. });
  21.  
  22.  
  23. $products = array_filter($products, function($val) {
  24.     $info = explode('|', $val);
  25.  
  26.     if(trim($info[1]) != $GLOBALS['filter'] && $GLOBALS['filter'] != 'all') {
  27.         return false;
  28.     }
  29.     if(trim($info[3]) < $GLOBALS['minPrice'] || trim($info[3]) > $GLOBALS['maxPrice']) {
  30.         return false;
  31.     }
  32.     return true;
  33. });
  34.  
  35. $products = array_combine(range(0, count($products) - 1), $products);
  36.  
  37. for($i = 0; $i < count($products) ;$i++) {
  38.     $currLine = explode('|', $products[$i]);
  39.  
  40.     $currLine = array_filter($currLine, function($val) {
  41.         if($val == "") {
  42.             return false;
  43.         }
  44.         return true;
  45.     });
  46.  
  47.     $newEntry = new stdClass();
  48.     $newEntry->name = trim($currLine[0]);
  49.     $newEntry->type = trim($currLine[1]);
  50.     $newEntry->comps = trim($currLine[2]);
  51.     $newEntry->price = trim(number_format(floatval($currLine[3]), 2, '.', ''));
  52.     $newEntry->id = trim($currLine[4]);
  53.     $arrayOfProducts[] = $newEntry;
  54. }
  55.  
  56. usort($arrayOfProducts, function($s1, $s2) use ($order, $property) {
  57.     if ($s1->$property == $s2->$property) {
  58.         return strcmp($s1->id, $s2->id);
  59.     }
  60.     return ($order == "ascending" ^ $s1->$property < $s2->$property) ? 1 : -1;
  61. });
  62.  
  63. for($i = 0; $i < count($arrayOfProducts) ;$i++) {
  64.     $components = explode(", ", $arrayOfProducts[$i]->comps);
  65.  
  66.     echo '<div class="product" id="product' . $arrayOfProducts[$i]->id . '">';
  67.     echo '<h2>' . htmlspecialchars(trim($arrayOfProducts[$i]->name))  . '</h2>';
  68.     echo '<ul>';
  69.     foreach ($components as $comp) {
  70.         echo '<li class="component">' . htmlspecialchars(trim($comp)) . '</li>';
  71.     }
  72.     echo '</ul>';
  73.  
  74.     echo '<span class="price">' . htmlspecialchars(trim($arrayOfProducts[$i]->price)) . '</span>';
  75.     echo '</div>';
  76.  
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement