Advertisement
dimipan80

softuniTunes

Apr 28th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. <?php
  2. $artist = $_GET['artist'];
  3. $sortProperty = $_GET['property'];
  4. $order = $_GET['order'];
  5. $inputSongs = explode("\n", $_GET['text']);
  6.  
  7. $songsList = array();
  8. foreach ($inputSongs as $inputRow) {
  9.     $inputRow = preg_split('/\s*\|\s*/', $inputRow, -1, PREG_SPLIT_NO_EMPTY);
  10.     $artists = preg_split('/,\s*/', $inputRow[2], -1, PREG_SPLIT_NO_EMPTY);
  11.     if (!in_array($artist, $artists)) {
  12.         continue;
  13.     }
  14.  
  15.     sort($artists);
  16.     $song = new stdClass();
  17.     $song->name = $inputRow[0];
  18.     $song->genre = $inputRow[1];
  19.     $song->artists = implode(', ', $artists);
  20.     $song->downloads = (int)$inputRow[3];
  21.     $song->rating = (float)$inputRow[4];
  22.  
  23.     $songsList[] = $song;
  24. }
  25.  
  26. usort($songsList, function ($a, $b) use ($sortProperty, $order) {
  27.     if ($a->$sortProperty == $b->$sortProperty) {
  28.         return strcmp($a->name, $b->name);
  29.     } elseif ($order == 'ascending') {
  30.         return ($a->$sortProperty > $b->$sortProperty) ? 1 : -1;
  31.     } else {
  32.         return ($a->$sortProperty < $b->$sortProperty) ? 1 : -1;
  33.     }
  34. });
  35.  
  36. echo "<table>\n<tr><th>Name</th><th>Genre</th><th>Artists</th><th>Downloads</th><th>Rating</th></tr>\n";
  37. foreach ($songsList as $song) {
  38.     $name = htmlspecialchars($song->name);
  39.     $genre = htmlspecialchars($song->genre);
  40.     $artists = htmlspecialchars($song->artists);
  41.     $downloads = htmlspecialchars($song->downloads);
  42.     $rating = htmlspecialchars($song->rating);
  43.     echo "<tr><td>{$name}</td><td>{$genre}</td><td>{$artists}</td><td>{$downloads}</td><td>{$rating}</td></tr>\n";
  44. }
  45. echo '</table>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement