jsschmitt Posted May 21, 2009 Share Posted May 21, 2009 This is the usage: Music Player w/ Playlists This is the issue: I am building a music player, that allows the creation of playlists. The issue lies in reading from the form when multiple songs are selected. This is the method: createplist.php contains a form that is built using a directory script. When the tracks are selected, and submitted, create.php makes a .xml file that contains the list of tracks. This is the code: createplist.php <?php /* $url = "http://www.anomymage.com/tests/do/music/"; */ $path = "music/"; print "<form method='post' action='create.php'>\n"; print "<input type='text' name='plistname' value='Playlist Name' /><br/>\n"; $narray=array(); $dir_handle = @opendir($path) or die("Unable to open $path"); $i=0; while($file = readdir($dir_handle)) { if($file != '.' && $file != '..' ) { //echo "<a href='$path/$file'>$file</a>"; $narray[$i]=$file; $i++; } } sort($narray); for($i=0; $i<sizeof($narray); $i++) { echo "<input type='checkbox' name='song' value='".$narray[$i]."'>".$narray[$i]."</input><br/>\n"; } //closing the directory closedir($dir_handle); print "<input type='submit' value='>'>\n"; print "</form>\n"; ?> create.php <?php $name = $_POST['plistname']; $begin = "<xml><track><path>http://www.anomymage.com/tests/do/music/music/"; $path = $_POST['song']; $mid = "</path><title>"; $title = $_POST['song']; $end = "</title></track></xml>"; $file = "playlists/".$name.".xml"; $Saved_File = fopen($file, 'w'); fwrite($Saved_File, $begin.$path.$mid.$title.$end); fclose($Saved_File); echo "Playlist <strong>".$name."</strong> created.<br /> <a href='../music/'>Back</a>"; ?> Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 21, 2009 Author Share Posted May 21, 2009 No one has any ideas? Quote Link to comment Share on other sites More sharing options...
Hybride Posted May 21, 2009 Share Posted May 21, 2009 It would be great to know what the issue actually is. What happens when multiple songs are selected? Looking at your second code page, my guess is that you're going to need a loop to loop through all the selected songs, since all it looks like is that it's taking just the first song. Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 21, 2009 Author Share Posted May 21, 2009 Good point... and that is the issue... It only grabs one song... even if multiple are checked! Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 21, 2009 Author Share Posted May 21, 2009 It would be great to know what the issue actually is. What happens when multiple songs are selected? Looking at your second code page, my guess is that you're going to need a loop to loop through all the selected songs, since all it looks like is that it's taking just the first song. Being a noob and all... any help on how to do that? Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 22, 2009 Author Share Posted May 22, 2009 Wow... no one can help me out? Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted May 22, 2009 Share Posted May 22, 2009 Well, maybe I'm just missing how you're doing it... but if you had multiple checkboxes, wouldn't you assume you need a loop of some sort in create.php to loop through and get the value from each checkbox? For instance... if "song" is the checkboxes, something like $begin = "<xml><track>"; $name = $_POST['plistname']; foreach ($_POST['song'] as $v) { $path = $_POST['song']; $mid = "<path>http://www.anomymage.com/tests/do/music/music/"</path><title>"; $title = $_POST['song']; $titleclose = "</title>"; } $end = "</track></xml>"; Quote Link to comment Share on other sites More sharing options...
Dathremar Posted May 22, 2009 Share Posted May 22, 2009 I would suggest setting the name of the input to : <input type='checkbox' name='song[]' value='".$narray[$i]."'>".$narray[$i]."</input> So it would be an array when You submit it. Also when You get the variable on the other side $song_array = $_POST['song']; for ($i=0; $i < count($song_array); $i++) { // print out the xml } Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 22, 2009 Author Share Posted May 22, 2009 What would I change the following to: $path = $_POST['song']; $title = $_POST['song']; Nevermind... figured it out... $path = $song_array[$i]; $title = $song_array[$i]; Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 22, 2009 Author Share Posted May 22, 2009 Updated code below, but now i get the following error: Warning: fwrite(): 3 is not a valid stream resource in /home/jschmitt/public_html/anomy/tests/do/music/create.php on line 29 Warning: fclose(): 3 is not a valid stream resource in /home/jschmitt/public_html/anomy/tests/do/music/create.php on line 30 <?php $song_array = $_POST['song']; $name = $_POST['plistname']; $bxml = "<xml>"; $file = "playlists/".$name.".xml"; $Saved_File = fopen($file, 'a'); fwrite($Saved_File, $bxml); fclose($Saved_File); for ($i=0; $i < count($song_array); $i++) { $begin = "<track><path>http://www.anomymage.com/tests/do/music/music/"; $path = $song_array[$i]; $mid = "</path><title>"; $title = $song_array[$i]; $end = "</title></track>"; $content = $begin.$path.$mid.$title.$end; fwrite($Saved_File, $content); fclose($Saved_File); }; $exml = "</xml>"; fwrite($Saved_File, $exml); fclose($Saved_File); echo "Playlist <strong>".$name."</strong> created.<br /> <a href='../music/'>Back</a>"; ?> Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 22, 2009 Author Share Posted May 22, 2009 Nevermind... fixed that as well.. Full code (that now works) below: <?php $song_array = $_POST['song']; $name = $_POST['plistname']; $bxml = "<xml>"; $file = "playlists/".$name.".xml"; if (file_exists($file)) { echo "The playlist ".$file." already exists"; } else { $Saved_File = fopen($file, 'a'); fwrite($Saved_File, $bxml); fclose($Saved_File); for ($i=0; $i < count($song_array); $i++) { $begin = "<track><path>http://www.anomymage.com/tests/do/music/music/"; $path = $song_array[$i]; $mid = "</path><title>"; $title = $song_array[$i]; $end = "</title></track>"; $content = $begin.$path.$mid.$title.$end; $file = "playlists/".$name.".xml"; $Saved_File = fopen($file, 'a'); fwrite($Saved_File, $content); fclose($Saved_File); }; $exml = "</xml>"; $file = "playlists/".$name.".xml"; $Saved_File = fopen($file, 'a'); fwrite($Saved_File, $exml); fclose($Saved_File); echo "Playlist <strong>".$name."</strong> created.<br /> <a href='../music/'>Back</a>";}; ?> Quote Link to comment Share on other sites More sharing options...
capabmx Posted September 8, 2009 Share Posted September 8, 2009 Hey just wondering, you built your own music player or what did you use? This is exactly the kind of thing I've been trying to make for my website but have had no avail, I'm using Drupal, and modifications to try and make a social networking site, so it would help great deal. Thanks for any info Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.