Advertisement
Filkolev

SoftUni iTunes

May 1st, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. <?php
  2. $text = preg_split("/\r?\n/", $_GET['text'], -1, PREG_SPLIT_NO_EMPTY);
  3. $artist = $_GET['artist'];
  4. $property = $_GET['property'];
  5. $order = $_GET['order'];
  6.  
  7. $tunesArray = [];
  8.  
  9. for ($i = 0; $i < count($text); $i++) {
  10.     $data = preg_split("/\s*\|\s*/", $text[$i], -1, PREG_SPLIT_NO_EMPTY);
  11.  
  12.     $tempTunes = [
  13.         'name' => trim($data[0]),
  14.         'genre' => trim($data[1]),
  15.         'artists' => explode(", ", trim($data[2])),
  16.         'downloads' => intval(trim($data[3])),
  17.         'rating' => floatval(trim($data[4]))
  18.     ];
  19.  
  20.     sort($tempTunes['artists']);
  21.  
  22.     if (in_array($artist, $tempTunes['artists'])) {
  23.         $tunesArray[] = $tempTunes;
  24.     }
  25. }
  26.  
  27. usort($tunesArray, function ($a, $b) use ($property) {
  28.     if ($property == 'genre' && strcmp($a[$property], $b[$property]) === 0) {
  29.         return strcmp($a['name'], $b['name']);
  30.     }
  31.  
  32.     if ($property == 'genre') {
  33.         return strcmp($a[$property], $b[$property]);
  34.     }
  35.  
  36.     if ($a[$property] === $b[$property]) {
  37.         return strcmp($b['name'], $a['name']);
  38.     }
  39.  
  40.     return $a[$property] < $b[$property] ? -1 : 1;
  41. });
  42.  
  43.     if ($order === 'descending') {
  44.         $tunesArray = array_reverse($tunesArray);
  45.     }
  46.  
  47.     echo "<table>\n";
  48.     echo "<tr><th>Name</th><th>Genre</th><th>Artists</th><th>Downloads</th><th>Rating</th></tr>\n";
  49.     for ($i = 0; $i < count($tunesArray); $i++) {
  50.         echo "<tr><td>" . htmlspecialchars($tunesArray[$i]['name']) .
  51.             "</td><td>" . htmlspecialchars($tunesArray[$i]['genre']) .
  52.             "</td><td>" . htmlspecialchars(implode(", ", $tunesArray[$i]['artists'])) .
  53.             "</td><td>" . htmlspecialchars($tunesArray[$i]['downloads']) .
  54.             "</td><td>" . htmlspecialchars($tunesArray[$i]['rating']) . "</td></tr>\n";
  55.     }
  56.     echo "</table>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement