Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $lines = $_GET['text'];
- $property = $_GET['property'];
- $artistInput = $_GET['artist'];
- $order = $_GET['order'];
- preg_match_all('/^.*$/m', $lines, $matches);
- $linesArr = array();
- for ($i = 0; $i < count($matches[0]); $i++) {
- $songInfo = explode(" | ", $matches[0][$i]);
- $linesArr[] = [
- 'name' => $songInfo[0],
- 'genre' => $songInfo[1],
- 'artist' => explode(", ", $songInfo[2]),
- 'downloads' => (int)$songInfo[3],
- 'rating' => floatval($songInfo[4])
- ];
- }
- //sort the names of the artists on each line
- $sortedArtistNames = [];
- foreach ($linesArr as $line) {
- asort($line['artist']);
- $line['artist'] = implode(", ", $line['artist']);
- $sortedArtistNames[] = $line;
- }
- //make a new array with only the lines that contain the name of the selected artist
- $artistChosen = [];
- foreach ($sortedArtistNames as $line) {
- foreach ($line as $key => $value) {
- if (strpos($value, $artistInput) !== false) {
- $artistChosen[] = $line;
- }
- }
- }
- //sort the lines by the given property
- usort($artistChosen, function($s1, $s2) use ($order, $property) {
- if ($s1[$property] == $s2[$property]) {
- return strcmp($s1['name'], $s2['name']);
- }
- return ($order == "ascending" ^ $s1[$property] < $s2[$property]) ? 1 : -1;
- });
- //print the result
- $output = "<table>\n<tr><th>Name</th><th>Genre</th><th>Artists</th><th>Downloads</th><th>Rating</th></tr>\n";
- for ($i = 0; $i < count($artistChosen); $i++) {
- $name = htmlspecialchars($artistChosen[$i]['name']);
- $genre = htmlspecialchars($artistChosen[$i]['genre']);
- $artist = htmlspecialchars($artistChosen[$i]['artist']);
- $downloads = $artistChosen[$i]['downloads'];
- $rating = $artistChosen[$i]['rating'];
- $output .= "<tr><td>$name</td><td>$genre</td><td>$artist</td><td>$downloads</td><td>$rating</td></tr>\n";
- }
- $output .= "</table>";
- echo $output;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement