• If you need help or want to discuss things, you now can also join us on our Discord Server!
  • A first preview of the unlimited version of SinusBot can be found in the Upcoming Changes thread. A version for Windows will follow, but we don't have a release date, yet.

EN |DE - PHP Sorting output

Status
Not open for further replies.

Kamikaze

Well-Known Member
Contributor
Hey Guys!

I need help by sorting the array of playlists. Here my sorting function (working)
PHP:
<?php
require("../login.php");

$playlist = $sinusbot->getPlaylists();

$playname = $playlist;

function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

aasort($playname,"name");

echo "<pre>";
print_r($playname);
echo "</pre>";

?>

upload_2016-11-28_13-50-2.png


So when I try to use it on my script to display playlist include tracks it don't want to work :(
The Script:
PHP:
<?php
require("../login.php");

$playlist = $sinusbot->getPlaylists();
for ($a = 0; $a < count($playlist); $a++) {
    $playuuid = $playlist[$a]["uuid"];
}

// Sort Array
function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

aasort($playname,"name");


$divCounter = 1;
for ($a = 0; $a < count($playlist); $a++) {
   
    $playname = $playlist[$a]["name"];
    $playuuid = $playlist[$a]["uuid"];
    $playlistTracks = $sinusbot->getPlaylistTracks($playuuid);
    $playlistTracks = $playlistTracks["entries"];
   
    If($a >= 1) {
        echo "<hr/>";
    }
    echo "
            <div class=header>
            <a class=collapsing href=# onclick=toggle_visibility('plisttracks$divCounter');>
                <div class=collapse>&#10084;</div>
                <h2>" . $playname . "</h2>
            </a>
            </div>";
    echo "<div id=plisttracks$divCounter class=plisttracks style=display:none;>";
   
    $divCounter++;
    $trackCounter = 1;
   
    for ($i = 0; $i < count($playlistTracks); $i++) {
       
        echo "<div id=filenr>" . $trackCounter . "</div>" . "<div id=filename>" . $playlistTracks[$i]["title"] . '</div>';
        $trackCounter++;
       
        //print_r($playlistTracks);
    }
    echo "</div>";
}
?>

The ouput is still random so no sorting.
Tried to put
PHP:
aasort($playname,"name");
on different places but still don't work.

Anyone could help me?
 

Kamikaze

Well-Known Member
Contributor
Solved it.
Didn't thought that the keys of the sorted array are still the same like
Code:
Array
(
    [9] => AAA
    [10] => BBB
    [6] => CCC
)

Changed it with "$playlist = array_values($playlist);"

- Can be closed / Kann geschlossen werden -
 
Status
Not open for further replies.
Top