Advertisement
tockata

softUniTunes

Jan 9th, 2015
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.03 KB | None | 0 0
  1.     <?php
  2.     $lines = preg_split('/\n/', $_GET['text']);
  3.     $artist = $_GET['artist'];
  4.     $property = $_GET['property'];
  5.     $order = $_GET['order'];
  6.    
  7.     $songs = [];
  8.    
  9.     for ($i = 0; $i < count($lines); $i++) {
  10.         $line = preg_split('/\s*\|\s*/', $lines[$i], -1, PREG_SPLIT_NO_EMPTY);
  11.         $name = trim($line[0]);
  12.         $genre = trim($line[1]);
  13.         $tempArr = preg_split('/\s*,\s*/', $line[2], -1, PREG_SPLIT_NO_EMPTY);
  14.         asort($tempArr);
  15.         $downloads = trim($line[3]);
  16.         $rating = floatval(trim($line[4]));
  17.         if (in_array($artist, $tempArr) && count($line) == 5) {
  18.             $songs[] = ['name' => $name, 'genre' => $genre, 'artists' => $tempArr, 'downloads' => $downloads, 'rating' => $rating];
  19.         }
  20.     }
  21.    
  22.     if (count($songs) > 0) {
  23.     //get sort keys:
  24.         foreach ($songs as $key => $row) {
  25.         $sortProperty[$key] = $row[$property];
  26.         $sortSongNames[$key] = $row['name'];
  27.         }
  28.         // Sort the data:
  29.         if ($order == 'ascending') {
  30.             array_multisort($sortProperty, SORT_ASC, $sortSongNames, SORT_ASC, $songs );
  31.         } else {
  32.             array_multisort($sortProperty, SORT_DESC, $sortSongNames, SORT_ASC, $songs );
  33.         }
  34.  
  35.         echo '<table>' . "\n";
  36.         echo '<tr><th>Name</th><th>Genre</th><th>Artists</th><th>Downloads</th><th>Rating</th></tr>' . "\n";
  37.         foreach ($songs as $song => $data) {
  38.             echo '<tr>';
  39.             echo "<td>" . htmlspecialchars($data['name']) ."</td><td>" . htmlspecialchars($data['genre']) .
  40.                     "</td><td>" . htmlspecialchars(implode(', ', $data['artists'])) . "</td><td>" .
  41.                     htmlspecialchars($data['downloads']) . "</td><td>" . htmlspecialchars($data['rating']) . "</td>";
  42.             echo '</tr>' . "\n";
  43.         }
  44.         echo '</table>';
  45.     } else {
  46.         echo '<table>' . "\n";
  47.         echo '<tr><th>Name</th><th>Genre</th><th>Artists</th><th>Downloads</th><th>Rating</th></tr>' . "\n";
  48.         echo '</table>';
  49.     }
  50.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement