Kirilas
Kirilas

Reputation: 63

Using PHP in a Javascript music player playlist

I've been working on a music player that's quite simple and to add music to it you would have to upload it to Dropbox and then manually edit the file (in this case index.php) where the playlist is held.The player then plays the links. But what I've done is made a file which inserts value through mysql into the database.Two columns:

songname, url

Index.php:

`$(document).ready(function(){

var myPlaylist = new jPlayerPlaylist({
    jPlayer: "#jquery_jplayer_N",
    cssSelectorAncestor: "#jp_container_N"
}, [
{
        title:"C O O L",
        artist:"Le Youth",
        mp3:"this is where the link must sit",
},`

How can I implement PHP query that selects the name and the link from database into that part of javascript code?

I'm sorry if there's some unclear things for you, please ask I will try to make everything clear.

Upvotes: 3

Views: 1449

Answers (2)

Shomz
Shomz

Reputation: 37701

PHP is a server-side language and it dies after it renders the page. So, you have two good options here.

First one is to grab all the links/names from the database, and then echo that into a JS object (using JSON seems the easiest way to handle the conversion), and then just call the link you need from that JS object. You can build the whole title/artist/mp3 object using PHP and be good to go. It should look something like this:

var mySonglist = <?php echo json_encode($databaseData) ?>;

The other option would require making AJAX calls to retrieve the link of the selected mp3. Although this might seem closer to what you're asking, due to its speed (it makes another server call), I'd suggest you do it only if you have a really, really huge number of songs at once.

So, the bottom line is: extend your PHP functionality to grab everything you need from the database, all the data, and then put that data into a JS variable which you will use to configure your player.

Upvotes: 1

Vignesh
Vignesh

Reputation: 1063

I am not sure that you can do db queries inside your script functions, But Do the queries outside the function and pass the php variables as arguments for the function. Below is an example.

<?php
$var="Select query here";
$name=$var[0]; $url = $var[1];
?>
<script>
play('$name','$url');

function play(name,url);
{
//your code goes here..
}
</script>

Upvotes: 0

Related Questions