We got a project for school were i need to create an mp3 album playlist using a premade PHP api using javascript or jquery only (Not allowed to use php). I can enter the data using an ajax call. I need to be able to enter more than one song with its url. I managed to enter the data this way to the DB into a column named songs.:
[{"name":["song1","song2"],"url":["url1","url2"]}]
How do I loop through this using Javascript or jQuery and showing it as a list?
This is the ajax call I am using.
function getsongs(index, name, url){
$.ajax({
url: "api/playlist.php?type=songs&id=" + index,
method: 'GET',
data: {
"songs": [
{
"name": name,
"url": url
},
] },
success: function(response, playlist){
// Need to loop here?
},
error: function(xhr){
console.log('error')
console.log(xhr)
}
}); }
Thank you.
forloop and use the same index variable to access each e.g.response[0].name[i](where i=0) will get you "song1" andresponse[0].url[i]would get you url1. But you might be better with an object structure so each song is a single object with all its properties, e.g.[{ "name": "song1", "url": "url1" }, { "name": "song2", "url": "url2" }]