Please help me with the following code, form data ‘title, artist, duration and song’ from the previous page needs to be added to xml file ‘playlist.xml’ which is used as a playlist. To remove a song works 100%, but when a song is added something goes wrong and previous data are removed. Can someone help me PLEASE!!!
[php]
<?php $songs = Array(); function start_element($parser, $name, $attrs){ global $songs; if($name == "title"){ 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 ""; if($_POST['action'] == "ins"){ array_push($songs, Array( "title" => $_POST['name'], "artist" => $_POST['artist'], "duration" => $_POST['duration'], "song" => $_POST['song'])); $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 = "\n"; foreach($songs_final as $song){ $write_string .= "\n$song[title]\n$song[artist]\n$song[duration]\n$song[song]\n\n"; } $write_string .= ""; $fp = fopen("playlist.xml", "w+"); fwrite($fp, $write_string) or die("Error writing to file"); fclose($fp); print "Song inserted or deleted successfully :)
"; print "Return"; ?>
[/php]