I am trying to create a foreach which will cycle through a given list of YouTube videos in order to populate a JW Player playlist. The structure of JW Player’s syntax allows for the inclusion of multiple videos to build a playlist, however I need to break into the foreach when it reaches the final item in order to close the playlist config.
When not being dynamically generated, the JW Player syntax looks so:
<div id='player_1465'></div>
<script type='text/javascript'>
jwplayer("player_1465").setup({
height: 360,
listbar: {
position: 'right',
size: 320
},
width: 960,
playlist: [{
sources: [
{ file: "http://www.youtube.com/watch?v=DeumyOzKqgI" }
],
title: "Skyfall Lyric Video"
},{
sources: [
{ file: "http://www.youtube.com/watch?v=diP-o_JxysA" }
],
title: "Star Trek Into Darkness Announcement Trailer"
}]
});
</script>
As you can see, each ‘source’ in the playlist is closed with ‘},{‘, with the last one ending with ‘}]’. I need the foreach to know when it has loaded the final video from the given array and close the playlist section with ‘}]’.
I have got so far with the dynamically generated code:
$clips = $tmdb->getMovieTrailers($tmdb_id);
<div id='player_1465'></div>
<script type='text/javascript'>
jwplayer("player_1465").setup({
height: 360,
listbar: {
position: 'right',
size: 320
},
width: 960,
playlist: [{
<?php
foreach($clips['youtube'] as $yt){
echo 'sources: [
{ file: "http://www.youtube.com/watch?v=' . $yt['source'] . '" }
],
title: "' . $yt['name'] . '"
}]'
}
?>
}]
});
</script>
Any help given will be greatly appreciated. Thank you!
You should consider using json_encode for this.