Page 1 of 1

Trying to make a playlist in php

Posted: Tue Jan 05, 2010 11:53 am
by jcafaro10
I'm trying to make something resembling a playlist in php. I'm really new to php but not programming so I have some ideas I'm just wondering what the best thing to do would be.

Here's what I have so far:

Code: Select all

 
<html>
 
<h1>My Music</h1>
 
<p align=center>
<?php
if (isset($_POST['submit'])) 
{
    $selected_song = $_POST['song'];
    print "Now playing $selected_song <br>";
}
?>
<OBJECT ID="MediaPlayer1" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab# Version=5,1,52,701" STANDBY="Loading Microsoft Windows® Media Player components..." TYPE="application/x-oleobject" width="280" height="46">
<param name="fileName" value="music/<?php print $selected_song; ?>">
<param name="animationatStart" value="true">
<param name="transparentatStart" value="true">
<param name="autoStart" value="true">
<param name="showControls" value="true">
<param name="Volume" value="-300">
<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="music/<?php print $selected_song; ?>" name="MediaPlayer1" width=280 height=46 autostart=1 showcontrols=1 volume=-300>
</OBJECT> 
</p>
 
 
<form name="myform" action="" method="POST">
<input type = submit name = "submit" value="Play Selection"><br>
<?php
if ($handle = opendir('music'))
{
    while (false !== ($file = readdir($handle))) 
    {
        if ($file != "." && $file != "..")
        {
                echo "<input type=radio name=song value=\"$file\"><a href=\"music/$file\">$file</a><br>\n";
              }
    }
 
    closedir($handle);
}
?>
</form>
</html>
 
Basically it just lists all of the songs as a radio button and you can pick which one to stream. I'd like to have some kind of playlist feature though. I was thinking of adding a checkbox next to each one and an add to playlist button. Then I'm not too sure what I would do once that posted but I was thinking I could add them all to an m3u file and have the player play the m3u file. Then i'd need some organization for different playlists

Re: Trying to make a playlist in php

Posted: Tue Jan 05, 2010 12:47 pm
by AbraCadaver
I might put the songs in a multiple select input and when posted save the songs in a file in the playlists directory. If you don't want to save the playlists, then you can put it in a session array and use that to spit out the m3u file. I don't know much about playlists, but it should be fairly simple.

Re: Trying to make a playlist in php

Posted: Tue Jan 05, 2010 12:49 pm
by jcafaro10
Thanks, right now I'm trying to just get checkboxes to work. Here's what it looks like now:

Code: Select all

 
<html>
 
<h1>My Music</h1>
 
<p align=center>
<?php
if (isset($_POST['submit'])) 
{
    $selected_song = $_POST['song'];
    print "Now playing $selected_song <br>";
}
if (isset($_POST['playlist']))
{
    echo "Selected songs:<br>";
    $songs=$_POST['songs'];
 
    while (list ($key,$val) = @each ($songs)) 
    {
        echo "$val<br>";
    } 
}
?>
<OBJECT ID="MediaPlayer1" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab# Version=5,1,52,701" STANDBY="Loading Microsoft Windows® Media Player components..." TYPE="application/x-oleobject" width="280" height="46">
<param name="fileName" value="music/<?php print $selected_song; ?>">
<param name="animationatStart" value="true">
<param name="transparentatStart" value="true">
<param name="autoStart" value="true">
<param name="showControls" value="true">
<param name="Volume" value="-300">
<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="music/<?php print $selected_song; ?>" name="MediaPlayer1" width=280 height=46 autostart=1 showcontrols=1 volume=-300>
</OBJECT> 
</p>
 
<form name="playlistform" action="" method="POST">
Select songs for playlist then <input type=submit name="playlist" value="Play All Selected"><br>
<?php
if ($handle = opendir('music'))
{
    while (false !== ($file = readdir($handle))) 
    {
        if ($file != "." && $file != "..")
        {
                echo "
            <input type=checkbox name=songs[] value=\"$file\">
            <form name=\"songform\" action=\"\" method=\"POST\">    
                <input type=submit name=\"submit\" value=\"Play\"><a href=\"music/$file\">$file</a><input  type=hidden value=\"$file\" name=song>
            </form>
            \n";
              }
    }
 
    closedir($handle);
}
?>
</form>
</html>
 
but for some reason, nothing prints out...any idea why my checkbox array isn't working?

Here's the code relevant to checkboxes:

Code: Select all

 
<?php
if (isset($_POST['playlist']))
{
    echo "Selected songs:<br>";
    $songs=$_POST['songs'];
 
    while (list ($key,$val) = @each ($songs)) 
    {
        echo "$val<br>";
    } 
}
?>
<form name="playlistform" action="" method="POST">
Select songs for playlist then <input type=submit name="playlist" value="Play All Selected"><br>
<?php
if ($handle = opendir('music'))
{
    while (false !== ($file = readdir($handle))) 
    {
        if ($file != "." && $file != "..")
        {
                echo "
            <input type=checkbox name=songs[] value=\"$file\">
            <form name=\"songform\" action=\"\" method=\"POST\">    
                <input type=submit name=\"submit\" value=\"Play\"><a href=\"music/$file\">$file</a><input  type=hidden value=\"$file\" name=song>
            </form>
            \n";
              }
    }
 
    closedir($handle);
}
?>
</form>
 
It ONLY works if the only song I select is the first song. If I select the first song and other songs, it only lists the first song.

Re: Trying to make a playlist in php

Posted: Tue Jan 05, 2010 1:04 pm
by AbraCadaver
You may need quotes arround the var name, not sure name="songs[]". It's best practice anyway. What do you get if you add this to the top of the page:

Code: Select all

<?php print_r($_POST); ?>

Re: Trying to make a playlist in php

Posted: Tue Jan 05, 2010 1:11 pm
by jcafaro10
Quotes doesn't help, no matter which one I select it always shows the first one when I do your thing.

I also changed the loop to:

Code: Select all

 
    foreach ($songs as $key => $value) 
    {
                echo "$value<br/>";
        }
 
and it still only works if I select the first one otherwise it gives me an error

Re: Trying to make a playlist in php

Posted: Tue Jan 05, 2010 1:19 pm
by AbraCadaver
Sorry I wasn't paying close enough attention. You can not use nested forms in HTML. You can have multiple forms, but not one inside of another.

Re: Trying to make a playlist in php

Posted: Tue Jan 05, 2010 1:33 pm
by jcafaro10
Oh...ok, then how is it possible to do what I'm trying to do. I want the ability to add songs to a playlist (use checkboxes) but also play individual songs. I don't think I can tie anything to a button can I?

Example, this part:

Code: Select all

 
<input type=\"submit\" name=\"submit\" value=\"Play\"><a href=\"music/$file\">$file</a><input  type=\"hidden\" value=\"$file\" name=\"song\">
 
This won't work because the hidden will just take the last value. How can I associate the hidden value with the button?

Re: Trying to make a playlist in php

Posted: Tue Jan 05, 2010 1:51 pm
by AbraCadaver
Several ways to go about it. You can put the value in the submit button, or you could use the link that you already have and pass a get value. I didn't test any of these:

First way:

Code: Select all

if (isset($_POST['song']))
{
    $selected_song = $_POST['song'];
    print "Now playing $selected_song <br>";
}
//rest of code snipped
 echo "
 <input type=\"checkbox\" name=\"songs[]\" value=\"$file\">
 <input type=\"submit\" name=\"song\" value=\"$file\"><a href=\"music/$file\">$file</a><br />";
Second way:

Code: Select all

if (isset($_GET['song']))
{
    $selected_song = $_GET['song'];
    print "Now playing $selected_song <br>";
}
//rest of code snipped
 echo "
 <input type=\"checkbox\" name=\"songs[]\" value=\"$file\">
 <a href=\"" . $_SERVER['PHP_SELF'] . "?song=$file\">$file</a><br />";
Maybe a third way using the filename as an array key of the song button. Lets you keep the button as Play:

Code: Select all

if (isset($_POST['song']))
{
    $selected_song = key($_POST['song']);
    print "Now playing $selected_song <br>";
}
//rest of code snipped
 echo "
 <input type=\"checkbox\" name=\"songs[]\" value=\"$file\">
 <input type=\"submit\" name=\"song[$file]\" value=\"Play\"><a href=\"music/$file\">$file</a><br />";

Re: Trying to make a playlist in php

Posted: Tue Jan 05, 2010 4:52 pm
by jcafaro10
Wow I didn't even know you can do that! To be honest, I'm not quite sure if I understand how it works, case in point I've run into a bug that I've found while using it but can't really figure out why it occurs:

Code: Select all

 
<html>
<p align=center>
<?php
if (isset($_POST['playlist']))
{
    echo "Selected songs:<br>";
    $songs=$_POST['songs'];
 
    foreach ($songs as $key => $value) 
    {
                echo "$value<br/>";
        }
 
}
elseif (isset($_POST['songs'])) 
{
    $selected_song = key($_POST['songs']);
    print "Now playing $selected_song <br>";
}
?>
 
<form name="playlistform" action="" method="POST">
Select songs for playlist then <input type=submit name="playlist" value="Play All Selected"><br>
<?php
if ($handle = opendir('music'))
{
    while (false !== ($file = readdir($handle))) 
    {
        if ($file != "." && $file != "..")
        {
                echo "
            <input type=\"checkbox\" name=\"songs[]\" value=\"$file\">
            <input type=\"submit\" name=\"songs[$file]\" value=\"Play\"><a href=\"music/$file\">$file</a>
            <br>
            \n";
              }
    }
 
    closedir($handle);
}
?>
</form>
</html>
 
The problem occurs if I select a few checkboxes and THEN hit a play button. In the simplest case, I check one box, and hit THAT play button. My understanding was that when I just had checkboxes songs[] was the array and $file was different values in the array, with keys being 0,1,2 etc. But now that I have songs[$file] it looks like I'm indexing an array by a value or something...

Re: Trying to make a playlist in php

Posted: Tue Jan 05, 2010 5:57 pm
by jcafaro10
I changed it from songs to song so that the names didn't conflict and that fixed the problem although I'm still not completely sure why.

I also got my playlist feature to work (sort of). It works the first time. Here's the code to make the playlist

Code: Select all

 
if (isset($_POST['playlist']))
{
    $myPlayList = "playlist.m3u";
    if(file_exists($myPlayList))
    {
        unlink($myPlayList);
    }
    $myHandle = fopen($myPlayList, 'w') or die("can't open file");
    echo "Selected songs:<br>";
    $songs=$_POST['songs'];
 
    foreach ($songs as $key => $value) 
    {
                echo "$value<br/>";
        fwrite($myHandle, "music/".$value."\n");
        }
    fclose($myHandle);
    $selected_song = $myPlayList;
}
 
I can check the file and see that the playlist is indeed being deleted and a new one being made BUT the player itself retains the old playlist, as if its cached somewhere and doesn't realize that it should be deleted. Closing the browser fixed the problem but that's obviously not a good solution. Any ideas?

Re: Trying to make a playlist in php

Posted: Tue Jan 05, 2010 8:54 pm
by AbraCadaver
Good work so far! The playlist may be a cache issue, I'm not sure how the different media players do it or if it's a browser problem. Your code looks fine. Just some suggestions though to make things shorter/easier maybe.

When you use fopen() with 'w' it will clear the contents of the file if it exists and will create it if it doesn't exist. The same with file_put_contents(). Also, unless I have to, I never use fopen because file_get/file_put_contents works for most things. Also, good job with the foreach, but sometimes you don't need to loop, implode() rules.

Code: Select all

if (isset($_POST['playlist']))
{
    $myPlayList = "playlist.m3u";
  
    echo "Selected songs:<br>";
    echo implode("<br>", array_map('htmlentities', $_POST['songs']));  //htmlentities to be safe
 
    $songs = 'music/' . implode("\nmusic/", $_POST['songs']);
    file_put_contents($myPlayList, $songs) or die("can't open file");
    $selected_song = $myPlayList;
}