I got this form that exports data and attached a piece of php code that enters the data into a xml file.
So far I managed to get the data wanted into the XML file but only at wrong spot.
Lets start with sample of XML file.
<?xml version="1.0" encoding="UTF-8"?>
<playlist xmlns="http://xspf.org/ns/0/" version="1">
<trackList>
<track>
<title>Episode 1</title>
<location>./Movies/krex/krex01.mp4</location>
<image>./Movies/krex/krex01.jpg</image>
</track>
<track>
<title>Episode 2</title>
<location>./Movies/krex/krex02.mp4</location>
<image>./Movies/krex/krex02.jpg</image>
</track>
<track>
<title>Episode 3</title>
<location>./Movies/krex/krex03.mp4</location>
<image>./Movies/krex/krex03.jpg</image>
</track>
<track>
<title>Episode 4</title>
<location>./Movies/krex/krex04.mp4</location>
<image>./Movies/krex/krex04.jpg</image>
</track>
</trackList>
What happens when I use form to add next entry:
[code]<?xml version="1.0" encoding="UTF-8"?>
Episode 1
./Movies/krex/krex01.mp4
./Movies/krex/krex01.jpg
Episode 2
./Movies/krex/krex02.mp4
./Movies/krex/krex02.jpg
Episode 3
./Movies/krex/krex03.mp4
./Movies/krex/krex03.jpg
Episode 4
./Movies/krex/krex04.mp4
./Movies/krex/krex04.jpg
As you can see the following entry is entered at the bottom.
What I want is it to be added after the last track in between and
As for PHP code handling the code:
[php]<?php
if (isset($_POST[‘submit’])) {
$fname = ($_POST[‘playlist’]);
$title = ($_POST[‘title’]);
$location = ($_POST[‘location’]);
$name = ($_POST[‘name’]);
echo $title;
echo “$fname”;
$xml = simplexml_load_file(“xml/$fname.xml”); //This line will load the XML file.
$sxe = new SimpleXMLElement($xml->asXML()); //In this line it create a SimpleXMLElement object with the source of the XML file.
//The following lines will add a new child and others child inside the previous child created.
$track = $sxe->addChild(“track”);
$track->addChild(“title”, “$title”);
$track->addChild(“location”, “$location$name.mp4”);
$track->addChild(“image”, “$location$name.jpg”);
$dom = new DOMDocument(‘1.0’);
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($sxe->asXML());
//Save XML to file - remove this and following line if save not desired
$dom->save(“xml/$fname.xml”);
}
?>[/php]
Php code might be bit messy but gets (most) of the job done.
Feel free to give general advice as to improve any codes in here as well though, I am always willing to learn, but most important thing to me at this moment is getting it located in right place.