0

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.

4
  • You've got an array containing a single object which contains two arrays. If you assume they're of equal length and that the entries at each index always correspond, then you can simply loop through the name array with a for loop and use the same index variable to access each e.g. response[0].name[i] (where i=0) will get you "song1" and response[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" }] Commented Aug 26, 2018 at 10:27
  • That would be a more conventional and easier-to-manage structure, and a better representation of each song. You could also then loop through it very easily. Commented Aug 26, 2018 at 10:28
  • The API is not editable. I have to use it in this manner. Commented Aug 26, 2018 at 14:30
  • That's a shame, because it's a silly, inefficient data structure. The answer below should provide what you're looking for while using that structure. Commented Aug 26, 2018 at 15:23

1 Answer 1

1

You can use "for" :

var arr = [{"name":["song1","song2"],"url":["url1","url2"]}];
var names = arr[0].name; // extract names from arr
var urls = arr[0].url; // extract urls from arr
for(var i=0; i< names.length && i < urls.length; i++){
   console.log(names[i]);
   console.log(urls[i]);
}

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you but that was not the question. I know how to use a loop. What I meant was, how do I convert the mysql data into a regular array since it is in a database. How do I retrieve it as an array on not as plain text?
@guy perhaps you're looking for JSON.parse()
@guy If arr is a text for convert it to array use this: arr = JSON.parse(arr);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.