Jump to content

pietrofxq

New Members
  • Posts

    2
  • Joined

  • Last visited

pietrofxq's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Sorry, but i didn't understand. I still don't see how i can create a property "musics" for each entry on the $playlists array with the songs of the playlist. This sql query returns me all the musics from all the playlists of the logged user, but how i'd do the loop without the query inside it?
  2. 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?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.