I have a table called "playlists", and a table called "musics". In the musics table, there is a column playlist_id which references the playlist that each music is contained.
I'm using api calls to display information on the site with JavaScript, so I need to return a JSON.
I need the json with the following structure:
[
Playlists: [
{
Name: "etc",
musics: [
{
name: "teste.mp3" },
{
name: "test2.mp3"
}
]
},
...
]
]
And this is my code:
$query = $con->prepare("SELECT * FROM playlists WHERE user_id = :id");
$query->execute(array("id" => $userID));
$playlists = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($playlists as &$key) {
$query = $con->prepare("SELECT * FROM musics WHERE playlist_id = :id");
$query->execute(array("id" => $key['ID']));
$songs = $query->fetchAll(PDO::FETCH_ASSOC);
$key['musics'] = $songs;
}
There's a way to avoid this loop?