Passing a PHP Array into jplayer playlist

573 views Asked by At

I am a beginner programmer and I'm trying to create a dynamically populated playlist from files in a folder. I have found a result here Dynamically populate playlist with JSON from PHP in jPlayer. This would allow me to do what I need.

However, I want to understand why my method does not work. I"m able to get an array into javascript using the myPlaylist variable that looks like the following from console.log.

Array [
    ""title":"Song1","artist":"Choir","mp3":"C:\xampp\htdocs\media\Song1.mp3"",
    ""title":"Song2","artist":"Choir","mp3":"C:\xampp\htdocs\media\Song2.mp3"",
    ""title":"Song3","artist":"Choir","mp3":"C:\xampp\htdocs\media\Song3.mp3"",
    ""title":"Song4","artist":"Choir","mp3":"C:\xampp\htdocs\media\Song4.mp3"", 
    ""title":"Song5","artist":"Choir","mp3":"C:\xampp\htdocs\media\Song5.mp3""
  ]

When I run this and jplayer playlist comes up in the browser it lists 5 items, but lists them all as undefined. Again, I"m sure I'm doing something stupid, but I can't figure out why they are all undefined.

Thanks in advance for your help!

1

There are 1 answers

5
apostl3pol On

In order to treat your playlist elements as objects, you need to wrap them with brackets instead of quotes. The final array output should look like this:

[
    {"title":"Song1","artist":"Choir","mp3":"C:\xampp\htdocs\media\Song1.mp3"},
    {"title":"Song2","artist":"Choir","mp3":"C:\xampp\htdocs\media\Song2.mp3"},
    {"title":"Song3","artist":"Choir","mp3":"C:\xampp\htdocs\media\Song3.mp3"},
    {"title":"Song4","artist":"Choir","mp3":"C:\xampp\htdocs\media\Song4.mp3"},
    {"title":"Song5","artist":"Choir","mp3":"C:\xampp\htdocs\media\Song5.mp3"}
]

Also, I'm not sure how you're preparing the file paths, but you should know that \ is an escape character in JavaScript. You have to either "escape the escape character" by using \\ instead, or else use the escape() and unescape() functions to convert the file paths.