Edit XML nodes using php form-please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
morales_
Forum Newbie
Posts: 1
Joined: Thu Feb 12, 2009 9:14 am

Edit XML nodes using php form-please help

Post by morales_ »

hello everyone, i'm trying to make something like CMS for my Flash file, so i use XML and PHP. My flash reads XML and my PHP edit that XML file. I'm begginer in PHP world so i must ask you for help. I have downloaded some script and whit this script I can add/delete nodes in XML file. Example of this you can see online here: http://www.prva-gimnazija.edu.ba/list/index.php
Now i want to have form that EDIT existing nodes of my XML file. I hope so that you will help me.

here is my processForm.xml

Code: Select all

<?
$songs = Array();
function start_element($parser, $name, $attrs){
    global $songs;
    if($name == "song"){
        array_push($songs, $attrs);
    }
}
function end_element ($parser, $name){}
$playlist_string = file_get_contents("playlist.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($songs, Array(
                "title" => $_POST['name'],
                "artist" => $_POST['artist'],
                "path" => $_POST['path']));
    $songs_final = $songs;
}else if($_POST['action'] == "del"){
    $songs_final = Array();
    foreach($songs as $song){
        if($song['title'] != $_POST['name']){
            array_push($songs_final, $song);
        }
    }
}
$write_string = "<songs>";
foreach($songs_final as $song){
    $write_string .= "<song title=\"$song[title]\" artist=\"$song[artist]\" path=\"$song[path]\" />";
}
$write_string .= "</songs>";
$fp = fopen("playlist.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>";
?>
this is index.php

Code: Select all

</head>
<body>
<form action="processForm.php" method="post">
<fieldset>
<label for="name">Name:</label> <input type="text" id="name" name="name"/><br />
<label for="artist">Artist:</label><input type="text" id="artist" name="artist" /><br />
<label for="path">Path:</label> <input type="text" id="path" name="path" /> <br />
<select name="action">
<option value="ins">Insert</option>
<option value="del">Delete</option>
</select>
<input type="submit" />
</fieldset>
</form>
<p>Current entries:</p>
<?php
function start_tag($parser, $name, $attrs){
    global $table_string;
    $table_string .= "<tr><td>$attrs[title]</td><td>$attrs[artist]</td><td>$attrs[path]</td></tr>";
}
function end_tag($parser, $name){}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($parser, "start_tag", "end_tag");
$table_string = "<table>
        <tr><th>Title</th><th>Artist</th><th>Path</th></tr>";
xml_parse($parser, file_get_contents("playlist.xml")) or die("Error parsing XML file");
$table_string .= "</table>";
echo $table_string;
 
?>
</body>
</html>
this is xml file:

Code: Select all

<songs><song title="On Mercury" artist="Red Hot Chili Peppers" path="/red-hot-chili-peppers/on-mercury.mp3" /><song title="Universally Speaking" artist="Red Hot Chili Peppers" path="  /red-hot-chili-peppers/universally-speaking.mp3" /></songs>
Post Reply