Advertisement
Filkolev

Gosho Is Moving

Dec 22nd, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.06 KB | None | 0 0
  1. <?php
  2.  
  3. if (!isset($_GET['typeLuggage'])) {
  4.     echo "<ul></ul>";
  5. } else if (empty($_GET['luggage'])) {
  6.     echo "<ul></ul>";
  7. } else if (empty($_GET['room'])) {
  8.     echo "<ul></ul>";
  9. } else if (empty($_GET['minWeight'])) {
  10.     echo "<ul></ul>";
  11. } else if (empty($_GET['maxWeight'])) {
  12.     echo "<ul></ul>";
  13. } else {
  14.     $results = array();
  15.  
  16.     $typeLuggage = $_GET['typeLuggage'];
  17.     $luggage = $_GET['luggage'];
  18.     $room = $_GET['room'];
  19.     $minWeight = intval($_GET['minWeight']); // floatval ?
  20.     $maxWeight = intval($_GET['maxWeight']);// floatval ?
  21.  
  22.     $luggagePieces = preg_split("/C\|_\|/", $luggage, -1, PREG_SPLIT_NO_EMPTY);
  23.  
  24.     foreach ($luggagePieces as $currentEntry) {
  25.         $data = preg_split("/\s*;\s*/", $currentEntry, -1, PREG_SPLIT_NO_EMPTY);
  26.  
  27.         if (count($data) < 4) {
  28.             continue;
  29.         }
  30.  
  31.         $entryType = trim($data[0]);
  32.         $entryRoom = trim($data[1]);
  33.         $entryName = trim($data[2]);
  34.         $entryWeight = intval(trim($data[3]));
  35.  
  36.         if ($entryRoom == $room && array_search($entryType, $typeLuggage) !== false) { // strict?
  37.             if (!array_key_exists($entryType, $results)) {
  38.                 $results[$entryType] = [
  39.                     'name' => array(),
  40.                     'weight' => 0
  41.                 ];
  42.             }
  43.  
  44.             $results[$entryType]['name'][] = $entryName;
  45.             $results[$entryType]['weight'] += $entryWeight;
  46.         }
  47.     }
  48.  
  49.     ksort($results);
  50.     $toPrint = "<ul>";
  51.  
  52.     foreach ($results as $category=>$catData) {
  53.         if ($catData['weight'] < $minWeight || $catData['weight'] > $maxWeight) {
  54.             continue;
  55.         }
  56.  
  57.         $types = $catData['name'];
  58.         sort($types);
  59.  
  60.         $toPrint .= "<li><p>". htmlspecialchars($category)."</p>";
  61.         $toPrint .= "<ul><li><p>". htmlspecialchars($room). "</p>";
  62.         $toPrint .= "<ul><li><p>" . htmlspecialchars(implode(", ", $types)) . " - ". htmlspecialchars($catData['weight']) ."kg</p></li></ul></li></ul></li>";
  63.     }
  64.  
  65.     $toPrint .= "</ul>";
  66.     echo $toPrint;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement