rosierd Posted August 21, 2007 Share Posted August 21, 2007 <?php // Code is sloppy for learning purposes! //create short variable name $musicroot = 'http://'; $xml = '<XML>'; $playlist = '<Playlist>'; $song = 'Song'; $name = 'Name'; $space = ' '; $src = 'Src'; $test = 'Test'; ?> <?php // Array Based $music = array('test.mp3', 'test1.mp3', 'test2.mp3', 'test3.mp3', 'test4.mp3', 'test6.mp3', 'test5.mp3', 'test7.mp3', 'test8.mp3'); // shuffle($music); ?> <?php // $tracklist = array('Track 1', 'Track 2', 'Track 3', 'Track 4', 'Track 5', 'Track 6', 'Track 7', 'Track 8', 'Track 9'); // sort($tracklist); foreach ($tracklist as $key => $value) $outputstring = "<".$song.">\n".$space."<".$name.">".$value."</".$name.">\n<".$src.">".$test."</"$src">"; $fp = fopen("music.xml", 'a+b'); if (!$fp) { echo '<p><strong> Error in writing to file. ' .'Maybe a permissions problem.</strong></p></body></html>'; exit; } fwrite($fp, $outputstring, strlen($outputstring)); fclose($fp); ?> Ok MY code is bad as I am not great, but getting there The Purpose of this code is to fed an array of songs.. it is then shuffled so they are never similar and written to a file (music.xml) my playlist file so to make it structured like a playlist I had to add (Line 31) "<".$song.">\n".$space."<".$name.">".$value."</".$name.">\n<".$src.">".$test."</"$src">"; In the process of finishing it off but I keep getting the same error, before I had something working and outputting near to what I needed but I can't remember what I changed (Ok Yes I should save drafts ) Parse error: parse error, unexpected T_VARIABLE in http://blahblahblah on Line 31 which is quoted above, please can someone help, I am not asking for the code to be finished off but help would be appreciated and thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/ Share on other sites More sharing options...
lemmin Posted August 21, 2007 Share Posted August 21, 2007 You are missing the dots around $src ..."</"$src">"; should be: "</".$src.">"; Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330127 Share on other sites More sharing options...
rosierd Posted August 21, 2007 Author Share Posted August 21, 2007 I actually can't believe I was missing something like Anyhow it writes to the file successfully but I need it to write every song in the array this is what I am getting <Song> <Name>Track 9</Name> <Src>Test</Src> Just one song goes in so I need a way of it to write all the songs in the array and last question can someone help me with a means of it unlinking the file when the user goes back onto the page so in effect it then updates the play list with the new songs order.. Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330138 Share on other sites More sharing options...
lemmin Posted August 21, 2007 Share Posted August 21, 2007 Change this chunk: // sort($tracklist); foreach ($tracklist as $key => $value) $outputstring = "<".$song.">\n".$space."<".$name.">".$value."</".$name.">\n<".$src.">".$test."</"$src">"; $fp = fopen("music.xml", 'a+b'); if (!$fp) { echo '<p><strong> Error in writing to file. ' .'Maybe a permissions problem.</strong></p></body></html>'; exit; } fwrite($fp, $outputstring, strlen($outputstring)); fclose($fp); ?> to: // sort($tracklist); $fp = fopen("music.xml", 'a+b'); if (!$fp) { echo '<p><strong> Error in writing to file. ' .'Maybe a permissions problem.</strong></p></body></html>'; exit; } foreach ($tracklist as $key => $value) { $outputstring = "<".$song.">\n".$space."<".$name.">".$value."</".$name.">\n<".$src.">".$test."</"$src">"; fwrite($fp, $outputstring, strlen($outputstring)); } fclose($fp); ?> Everytime you use fopen() it should refresh the file and fclose() should effectively unlink it. Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330155 Share on other sites More sharing options...
rosierd Posted August 21, 2007 Author Share Posted August 21, 2007 Ok thank you once again your helping me a lot... But I need it to write <XML> <Playlist> open and closed tags to front and end of script so this is what I have but it's not writing those to the playlist so I couldn't bother you to help me! <?php // Code is sloppy for learning purposes! //create short variable name $musicroot = 'http://'; $xml = 'XML'; $playlist = 'Playlist'; $song = 'Song'; $name = 'Name'; $space = ' '; $src = 'Src'; $test = 'Test'; ?> <?php // Array Based $music = array('test.mp3', 'test1.mp3', 'test2.mp3', 'test3.mp3', 'test4.mp3', 'test6.mp3', 'test5.mp3', 'test7.mp3', 'test8.mp3'); // shuffle($music); ?> <?php //time to write <xml> <playlist> $fp = fopen("music.xml", 'wb'); if (!$fp) { echo '<p><strong> Error in writing to file. ' .'Maybe a permissions problem.</strong></p></body></html>'; exit; $outputstring = "<".$xml.">\n<".$playlist.">"; fwrite($fp, $outputstring, strlen($outputstring)); } fclose($fp); ?> <?php // $tracklist = array('Track 1', 'Track 2', 'Track 3', 'Track 4', 'Track 5', 'Track 6', 'Track 7', 'Track 8', 'Track 9'); // sort($tracklist); $fp = fopen("music.xml", 'a+b'); if (!$fp) { echo '<p><strong> Error in writing to file. ' .'Maybe a permissions problem.</strong></p></body></html>'; exit; } foreach ($tracklist as $key => $value) { $outputstring = "<".$song.">\n".$space."<".$name.">".$value."</".$name.">\n<".$src.">".$test."</".$src.">\n</".$song.">\n"; fwrite($fp, $outputstring, strlen($outputstring)); } fclose($fp); ?> <?php // Time to write to the end of the file a+b $fp = fopen("music.xml", 'a+b'); if (!$fp) { echo '<p><strong> Error in writing to file. ' .'Maybe a permissions problem.</strong></p></body></html>'; exit; $outputstring = "</".$playlist.">\n</".$xml.">"; fwrite($fp, $outputstring, strlen($outputstring)); } fclose($fp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330179 Share on other sites More sharing options...
lemmin Posted August 21, 2007 Share Posted August 21, 2007 You have the code that writes to the file inside the if statement that checks if the file is bad. change: if (!$fp) { echo '<p><strong> Error in writing to file. ' .'Maybe a permissions problem.</strong></p></body></html>'; exit; $outputstring = "<".$xml.">\n<".$playlist.">"; fwrite($fp, $outputstring, strlen($outputstring)); } to: if (!$fp) { echo '<p><strong> Error in writing to file. ' .'Maybe a permissions problem.</strong></p></body></html>'; exit; } $outputstring = "<".$xml.">\n<".$playlist.">"; fwrite($fp, $outputstring, strlen($outputstring)); Same thing with the one at the bottom. Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330187 Share on other sites More sharing options...
rosierd Posted August 21, 2007 Author Share Posted August 21, 2007 Right here is the output in the music.xml file <XML> <Playlist><Song> <Name>Track 1</Name> <Src>Test</Src> </Song> <Song> <Name>Track 2</Name> <Src>Test</Src> </Song> <Song> <Name>Track 3</Name> <Src>Test</Src> </Song> <Song> <Name>Track 4</Name> <Src>Test</Src> </Song> <Song> <Name>Track 5</Name> <Src>Test</Src> </Song> <Song> <Name>Track 6</Name> <Src>Test</Src> </Song> <Song> <Name>Track 7</Name> <Src>Test</Src> </Song> <Song> <Name>Track 8</Name> <Src>Test</Src> </Song> <Song> <Name>Track 9</Name> <Src>Test</Src> </Song> </Playlist> </XML> So far so good but rather than having <Src> Test </Src> I know it's test because I made it a variable just as an example but I need it to shuffle the music array as it does and then for each song input where $test is a song and then have it make the entries for every song so a loop or for each, for example, I hope you understand what I mean and can help, Thank you Once again! Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330193 Share on other sites More sharing options...
lemmin Posted August 21, 2007 Share Posted August 21, 2007 You can use the shuffle() function on your array to randomize the order. http://www.php.net/manual/en/function.shuffle.php Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330252 Share on other sites More sharing options...
rosierd Posted August 21, 2007 Author Share Posted August 21, 2007 I have used shuffle and it works but I need it to input the shuffled song locations into each of the seperate <src> tags Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330272 Share on other sites More sharing options...
lemmin Posted August 21, 2007 Share Posted August 21, 2007 It would be pretty complicated to do that using two different arrays. I would suggest putting the source into the array that has the track names, like this: $tracklist = array('Track 1' => 'c:\\file.mp3', 'Track 2' => 'c:\\file2.mp3', ... Then, in your loop, you can use $key instead of $value, and $value instead of $src. Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330284 Share on other sites More sharing options...
rosierd Posted August 21, 2007 Author Share Posted August 21, 2007 Ok Here is my finished script <?php // Code is sloppy for learning purposes! //create short variable name $xml = 'XML'; $playlist = 'Playlist'; $song = 'Song'; $name = 'Name'; $space = ' '; $src = 'Src'; $test = 'Test'; ?> <?php //time to write <xml> <playlist> $fp = fopen("music.xml", 'wb'); if (!$fp) { echo '<p><strong> Error in writing to file. ' .'Maybe a permissions problem.</strong></p></body></html>'; exit; } $outputstring = "<".$xml.">\n<".$playlist.">\n"; fwrite($fp, $outputstring, strlen($outputstring)); ?> <?php // $tracklist = array('Track 1' => '01.mp3', 'Track 2' => '02.mp3', 'Track 3' => '03.mp3', 'Track 4' => '04.mp3', 'Track 5' => '05.mp3', 'Track 6' => '06.mp3', 'Track 7' => '07.mp3', 'Track 8' => '08.mp3', 'Track 9' => '09.mp3', 'Track 10' => '10.mp3', 'Track 11' => '11.mp3', 'Track 12' => '12.mp3', 'Track 13' => '13.mp3', 'Track 14' => '14.mp3', 'Track 15' => '15.mp3', 'Track 16' => '16.mp3', 'Track 17' => '17.mp3', 'Track 18' => '18.mp3', 'Track 19' => '19.mp3', 'Track 20' => '20.mp3'); // sort($tracklist); $fp = fopen("music.xml", 'a+b'); if (!$fp) { echo '<p><strong> Error in writing to file. ' .'Maybe a permissions problem.</strong></p></body></html>'; exit; } foreach ($tracklist as $key => $value) { $outputstring = "<".$song.">\n".$space."<".$name.">".$key."</".$name.">\n<".$src.">".$value."</".$src.">\n</".$song.">\n"; fwrite($fp, $outputstring, strlen($outputstring)); } fclose($fp); ?> <?php // Time to write to the end of the file a+b $fp = fopen("music.xml", 'a+b'); if (!$fp) { echo '<p><strong> Error in writing to file. ' .'Maybe a permissions problem.</strong></p></body></html>'; exit; } $outputstring = "</".$playlist.">\n</".$xml.">"; fwrite($fp, $outputstring, strlen($outputstring)); ?> It comes up with this error Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in Line 26 Line 26: $tracklist = array('Track 1' => '01.mp3', 'Track 2' => '02.mp3', Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330376 Share on other sites More sharing options...
lemmin Posted August 21, 2007 Share Posted August 21, 2007 What version of PHP do you have? I'm not immediately noticing the error. There might be something that a lower version of PHP overlooks. It should work fine the way it is in php5, but try to remove the three lines above line 26, they aren't really doing anything. Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330393 Share on other sites More sharing options...
rosierd Posted August 21, 2007 Author Share Posted August 21, 2007 PHP Version 4.3.11 Also the 3 lines above are omitting <XML> <Playlist> Which are needed for the player eventually to populate with the songs... It's just giving me an error which I can't fix.. Edit: Everything worked fine until I started adding the links to the files.. Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330396 Share on other sites More sharing options...
rosierd Posted August 21, 2007 Author Share Posted August 21, 2007 Ok it seem's I have fixed the problem but I will leave this topic unsolved until I have added it to the player and it works it seem's it did not like a file name with ' for example and using 'x2 as in '' didn't help Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330402 Share on other sites More sharing options...
rosierd Posted August 21, 2007 Author Share Posted August 21, 2007 Ok 1 last question for sure... Once the file has been created can we print to screen "Playlist Created" etc. i.e using echo.. but after all that has been run (the code) can it redirect to another page but with header(....) so on... it can't have any text before it, or can it? and will it run after all the other code has been ran.. Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330404 Share on other sites More sharing options...
lemmin Posted August 21, 2007 Share Posted August 21, 2007 You can echo before sending the header, but you won't be able to read it. The only way to really display a "done message" would be to use the head to redirect to the "done message" page and have that page redirect to the next one. Or just echo out the necessary javascript on the current page to do this. Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330405 Share on other sites More sharing options...
rosierd Posted August 21, 2007 Author Share Posted August 21, 2007 Well Lemmin Thank you very much you have been a great help will make sure to sing your praises in the top post about being helped.. Good Luck in the future and with that topic solved as everything is working perfectly Quote Link to comment https://forums.phpfreaks.com/topic/66017-solved-need-help-writing-to-file-playlist/#findComment-330408 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.