Jump to content

[SOLVED] Need Help Writing To File (Playlist)


rosierd

Recommended Posts

<?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  :)

Link to comment
Share on other sites

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..

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.