PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
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
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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
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?
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:
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 />";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
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:
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...
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?
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.
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;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.