Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $text = $_GET['text'];
- $nameInput = $_GET['artist'];
- $property = $_GET['property'];
- echo $property;
- $order = $_GET['order'];
- $lines = preg_split('/[\n\r]+/',$text);
- foreach ($lines as $key => $match) {
- $ckech = strpos($match,$nameInput);
- if($ckech==0){
- unset($lines[$key]);
- }
- }
- $unsortedArray = [];
- $count = 0;
- foreach ($lines as $elements) {
- $splited = preg_split('/ \| /',$elements);
- $unsortedArray[$count]['name'] = $splited[0];
- $unsortedArray[$count]['genre'] = $splited[1];
- $unsortedArray[$count]['artists'] = $splited[2];
- $unsortedArray[$count]['downloads'] = $splited[3];
- $unsortedArray[$count]['rating'] = $splited[4];
- $count++;
- }
- $sortedArray = columnSort($unsortedArray,$property,$order);
- echo "<table>\n";
- echo "<tr><th>Name</th><th>Genre</th><th>Artists</th><th>Downloads</th><th>Rating</th></tr>\n";
- foreach ($sortedArray as $array) {
- $stringNames = $array['artists'];
- $sortedNames = sortNames($stringNames);
- echo "<tr>";
- echo "<td>".htmlspecialchars($array['name'])."</td>";
- echo "<td>".htmlspecialchars($array['genre'])."</td>";
- echo "<td>".htmlspecialchars($sortedNames)."</td>";
- echo "<td>".htmlspecialchars($array['downloads'])."</td>";
- echo "<td>".htmlspecialchars($array['rating'])."</td>";
- echo "</tr>\n";
- }
- echo "</table>\n";
- function sortNames($names){
- $namesArr = preg_split('/,/',$names);
- $trimedArr = array_map('trim',$namesArr);
- asort($trimedArr);
- $submited = implode(", ",$trimedArr);
- return $submited;
- }
- function columnSort($unsortedArray, $column,$sequence) {
- $sorted = $unsortedArray;
- if($sequence=="ascending") {
- for ($i = 0; $i < sizeof($sorted) - 1; $i++) {
- for ($j = 0; $j < sizeof($sorted) - 1 - $i; $j++)
- if ($sorted[$j][$column] > $sorted[$j + 1][$column]) {
- $tmp = $sorted[$j];
- $sorted[$j] = $sorted[$j + 1];
- $sorted[$j + 1] = $tmp;
- }
- }
- return $sorted;
- }
- else{
- for ($i = 0; $i < sizeof($sorted) - 1; $i++) {
- for ($j = 0; $j < sizeof($sorted) - 1 - $i; $j++)
- if ($sorted[$j][$column] < $sorted[$j + 1][$column]) {
- $tmp = $sorted[$j];
- $sorted[$j] = $sorted[$j + 1];
- $sorted[$j + 1] = $tmp;
- }
- }
- return $sorted;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement