I have a SQL query which get my playlist ordered by tag and name :
SELECT * FROM playlist WHERE iduser=9 ORDER BY tag,name ASC;
Result :
| iduser | idplaylist | name | tag |
|---|---|---|---|
| 9 | PL74A6S2w9aDA-Gdnhb7b58wRSjYo7t9LK | AlphaCast : Table Quest | Comedy |
| 9 | PL1UBPUKVQV3qV0o9W1Yy0H8krB1IvhC1R | AlphaCast : Baldurs gate 3 | Gaming |
| 9 | PL1UBPUKVQV3oRu-NanDuXxFxMUR1li9XU | AlphaCast : The Witcher 3 | Gaming |
| 9 | PLUUOiZvrsOA-vaRZTFWPPd4Xij_LhmePy | Lore Starcraft | Gaming |
I send this result row by row to a JavaScript function which aims to use the Youtube API and get a JSON for each idplaylist and send this JSON to another JavaScript function.
The problem is that when I use $.getJSON by idplaylist, the JSON I get are not in the same order than the table result above.
The code :
<?php
$sql = "SELECT * FROM playlist WHERE iduser=? ORDER BY tag,name ASC;";
mysqli_stmt_bind_param($stmt, "i", $_SESSION["iduser"]);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while($row = mysqli_fetch_assoc($result)){
$playlistid = $row['idplaylist'];
$playlistname = $row['name'];
$playlisttag = $row['tag'];
echo "<script>displayplaylist('$playlistid', '$playlistname', '$playlisttag');</script>";
}
?>
function displayplaylist(playlistid, playlistname, playlisttag) {
var key = '';
var playlistId = playlistid;
var URL = 'https://www.googleapis.com/youtube/v3/playlistItems';
var options = {
part: 'snippet',
key: key,
maxResults: 50,
playlistId: playlistId
}
$.getJSON(URL, options function (data) {
resultplaylist(data, playlistname, playlisttag);
resultvideos(data, playlistid);
});
}