Hello guys. Great forum, not just that but also members.
So I used one old script to load xml file from php form, posted long time ago here, and spend last weekend to made this work good, but somewhere I had success, somewhere don’t.
So I hoping some here have solution. For rest of the people who need this I will explaine in short what I have.
So this xml file I need to edit test.xml. Also it is possible that I have that in one line.
<?xml version="1.0" encoding="utf-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
<trackList>
<track>
<title>Example 1</title>
<creator>Joe Tribiani</creator>
<location>/roki/images/6c.png</location>
<info>index.php</info>
</track>
<track>
<title>Example 2</title>
<creator>Joe Tribiani</creator>
<location>/roki/images/27.png</location>
<info>forum</info>
</track>
</trackList>
</playlist>
This is my PHP processform.php
<?
$trackList = Array();
function start_element($parser, $name, $attrs){
global $trackList;
if($name == "track"){
array_push($trackList, $attrs);
}
}
function end_element ($parser, $name){}
$playlist_string = file_get_contents("test.xml");
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse($parser, $playlist_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "ins"){
array_push($trackList, Array(
"title" => $_POST['title'],
"creator" => $_POST['creator'],
"location" => $_POST['location'],
"info" => $_POST['info']));
$trackList_final = $trackList;
}else if($_POST['action'] == "del"){
$trackList_final = Array();
foreach($trackList as $track){
if($track['title'] != $_POST['title']){
array_push($trackList_final, $track);
}
}
}
//Write XML vers and enctype
$write_string .= '<?xml version="1.0" encoding="utf-8"?>';
// Write root tag open tag <root>
$write_string .= '<playlist version="1" xmlns="http://xspf.org/ns/0/">';
$write_string .= '<trackList>';
$write_string .= '<track>';
foreach($trackList_final as $track)
{
$write_string .= '<title>'.$track[title].'</title>';
$write_string .= '<creator>'.$track[creator].'</creator>';
$write_string .= '<location>'.$track[location].'</location>';
$write_string .= '<info>'.$track[info].'</info>';
}
// Write root tag close tag </root>
$write_string .= '</track>';
$write_string .= '</trackList>';
$write_string .= '</playlist>';
$fp = fopen("test.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Song inserted or deleted successfully :)<em><br />";
print "<a href=\"index.php\" title=\"return\">Return</a>";
?>
When I first time update test.xml with processForm.php, my test.xml is OK. I get what I need just everything is one line. No problem. Problem start when I do that second time. Than values what added first time dissapered and I have only in test.xml values from second update.
So test.xml look like this.
<?xml version="1.0" encoding="utf-8"?><playlist version="1" xmlns="http://xspf.org/ns/0/"><trackList><track><title></title><creator></creator><location></location><info></info><title>Example 2</title><creator>JoeTribiani</creator><location>/roki/images/27.png</location><info>forum</info></track></trackList></playlist>
Any help?
Thanx