Page 1 of 1

(RESOLVED) Delete from xml problem...

Posted: Mon Feb 22, 2010 5:27 am
by hairytea
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)

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>&nbsp;";
  
}
 
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());
}
 
?>
 
A snippet of the xml file...

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>
 

Re: Delete from xml problem...

Posted: Mon Feb 22, 2010 8:58 am
by hairytea
I have been testing the script by echo'ing the value held by $i !!!

When you click the images it simply shows the array number held by $i, but when I click on whatever is the last image oin the array it echoes NOTHING to the screen.

Why does the last item in the array not hold the value assigned in the loop??

Re: (RESOLVED) Delete from xml problem...

Posted: Mon Feb 22, 2010 9:36 am
by hairytea
I found the problem....

Code: Select all

 
foreach( $imageSet as $newSet )
{
  $count++;
  
  $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;
  
  $all .= "<a href='deleteResidential.php5?cnt=".$count."&del=".$image."'><img src='".$image."' width='150' height='106' alt='Delete: ".$title." Picture' title='Delete: ".$title." Picture' border='0' /></a>&nbsp;";
  
  if ($del == $image) {
    delete_image($image);
  }
  
}
 
All I did was move the counter variable to be the first thing to increment when the loop runs and also added the function call after populating the $all variable :-)

Shimples (squeak)