Please Help! Deleting XML node(s)
Posted: Sat Sep 12, 2009 7:51 pm
Hi all, I'm brand new to this forum and hope to contribute as well as learn as time goes on. I'm in the process of teaching myself PHP, so I may be missing something rather obvious to experienced coders (so please be lenient!)
As for this problem, I've done extensive research on the matter but cannot come up with a solution. I'm writing a image gallery manager editable easily by my clients. The admin uploads an image via PHP form, a script resizes it and adds an entry into an XML file. A flash gallery then reads the XML file and displays images for users. That part is all set up. The problem is when admin tries to delete image, everything works except deleting the XML reference to said image.
I suspect the problem lies in that each XML entry's tag is the same as the others. It's not <image1>, <image2>, etc as you can see. So I have to search by node value then delete said node and parent (I think?).
I receive no PHP errors from server when running this.
Below is 'gallery.xml'
checked[] refers to the array of images selected on previous page for deletion.
Thanks so much for any help,
oldnoisy
As for this problem, I've done extensive research on the matter but cannot come up with a solution. I'm writing a image gallery manager editable easily by my clients. The admin uploads an image via PHP form, a script resizes it and adds an entry into an XML file. A flash gallery then reads the XML file and displays images for users. That part is all set up. The problem is when admin tries to delete image, everything works except deleting the XML reference to said image.
I suspect the problem lies in that each XML entry's tag is the same as the others. It's not <image1>, <image2>, etc as you can see. So I have to search by node value then delete said node and parent (I think?).
I receive no PHP errors from server when running this.
Below is 'gallery.xml'
Code: Select all
<image>
<filename>1252534809.jpg</filename>
<caption>Fun time coding PHP!!</caption>
</image>
Code: Select all
if (isset($_POST['submit'])) {
$checked = $_POST["checked"];
$how_many = count($checked);
echo $how_many.' file(s) successfully deleted.';
if ($how_many>0) {
}
for ($i=0; $i<$how_many; $i++) {
if(file_exists('/home/content/m/u/c/muchomungo/html/phpmanager/images/'.$checked[$i])){
unlink('/home/content/m/u/c/muchomungo/html/phpmanager/images/'.$checked[$i]);
}
if(file_exists('/home/content/m/u/c/muchomungo/html/phpmanager/images/'.$checked[$i])){
unlink('/home/content/m/u/c/muchomungo/html/phpmanager/images/'.$checked[$i]);
}
}
//Remove XML reference to deleted image
$xmlfile = "gallery.xml";
$xmlstr = file_get_contents("$xmlfile");
$xml = new SimpleXMLElement($xmlstr);
$count = 0;
foreach ($xml as $image){
for ($i=0; $i<$how_many; $i++) {
if ($image->filename == $checked[i]){
unset($xml->image[$count]); break;
}
}
$count++;
}
$handle = fopen("$xmlfile", "w");
fwrite($handle, $xml->asXML());
fclose($handle);
echo $xml->asXML();
echo $xml->saveXML();
}
Thanks so much for any help,
oldnoisy