(RESOLVED) Delete from xml problem...
Posted: Mon Feb 22, 2010 5:27 am
Hi all,
(need help deleting the last image in xml array)
My script below performs the following functions...
1. Opens the xml file and reads contents
2. Displays each image (hyperlinked to reload page and set variables) to the screen
3. If the page loads and varibles are set the delete function is called
4. The delete function takes the variable $i and deletes the appropriate image set in the array (unset($data->imageSet[$i]);)
THE PROBLEM..
Everything works fine and the image set is deleted from the array perfectly, EXCEPT for the last image in the array. For some reason (no matter how many image sets there are in the array) it will never delete the last array element??
Any ideas why?
(i',m guessing it's something to do with the incrementing variable ($i) that is set, but not sure)
A snippet of the xml file...
(need help deleting the last image in xml array)
My script below performs the following functions...
1. Opens the xml file and reads contents
2. Displays each image (hyperlinked to reload page and set variables) to the screen
3. If the page loads and varibles are set the delete function is called
4. The delete function takes the variable $i and deletes the appropriate image set in the array (unset($data->imageSet[$i]);)
THE PROBLEM..
Everything works fine and the image set is deleted from the array perfectly, EXCEPT for the last image in the array. For some reason (no matter how many image sets there are in the array) it will never delete the last array element??
Any ideas why?
(i',m guessing it's something to do with the incrementing variable ($i) that is set, but not sure)
Code: Select all
<?php
if (!$_COOKIE["auth"] == "1") {
header("Location: login.php5?error=error");
}
$del = $_GET['del'];
$cnt = $_GET['cnt'];
$doc = new DOMDocument();
$doc->load( 'flash/commercial_images.xml' );
$count = 0;
$imageSet = $doc->getElementsByTagName( "imageSet" );
foreach( $imageSet as $newSet )
{
if ($del == $image) {
delete_image($image);
}
$images = $newSet->getElementsByTagName( "imageURL" );
$image = $images->item(0)->nodeValue;
$titles= $newSet->getElementsByTagName( "title" );
$title= $titles->item(0)->nodeValue;
$descs = $newSet->getElementsByTagName( "desc" );
$desc = $descs->item(0)->nodeValue;
$count++;
$all .= "<a href='delete.php5?cnt=".$count."&del=".$image."'><img src='".$image."' width='150' height='106' alt='Delete: ".$title." Picture' title='Delete: ".$title." Picture' border='0' /></a> ";
}
function delete_image($node, $filename = 'flash/commercial_images.xml'){
$data = simplexml_load_file($filename);
for($i = 0, $length = count($data->imageSet); $i < $length; $i++){
if($data->imageSet[$i]->imageURL == $node){
unset($data->imageSet[$i]);
break;
}
}
file_put_contents($filename, $data->saveXML());
}
?>
Code: Select all
<?xml version="1.0"?>
<galleries>
<imageSet>
<imageURL>flash/commercial_images/JJUK PICTURES 007.JPG</imageURL>
<title>sainsburys</title>
<desc>new sainsbury store shopfront </desc>
</imageSet>
</galleries>