Jump to content

Modify Script for XML Files


Liu

Recommended Posts

Hey guys

 

I have this code here, to add/delete xml File Entrys. But i need a Modification for it, which would add the new Entrys to the Top instead of the Bottom.

But since im not that good with php, nothing worked so far. Could someone help me out somehow?

 

Thanks for any kind of Help in advance!

Liu

 

 

processForm.php

$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>";
?>

 

index.php

<head>
<title>Add Songs</title>
<style type="text/css">
table {
border:1px dotted #ccc;
width:100%;
}
td {
	background-color:#aaa;
color:#fff;
}
fieldset { border:0; }
label { margin-right:5px; }

</style>
</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>

 

playlist.xml (example of the structure)

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

Link to comment
https://forums.phpfreaks.com/topic/239134-modify-script-for-xml-files/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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