...

View Full Version : Removing XML node(s) by value



oldnoisy
09-13-2009, 01:31 AM
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'


<image>
<filename>1252534809.jpg</filename>
<caption>Fun time coding PHP!!</caption>
</image>


checked[] refers to the array of images selected on previous page for deletion.




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

zactanaz
09-13-2009, 03:20 AM
Here, I made some modification your code.

Basically, you deleted the image file twice, why?. Second why not use simplexml_load_file() is easier that way. Also you save the XML file with asXML(), it creates files with one line of code not very readable from the human perceptive, specially when you have long files. Read about it: http://us2.php.net/manual/fr/function.simplexml-element-asXML.php.

What my code does it makes it more readable to a person by reconstructing the XML file, and not adding back the files you need to delete.


if(!empty($_POST['submit'])) {
$checked = $_POST['checked'];
$how_many = count($checked);

if ($how_many > 0) {
//Delete image files
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]);
}
}

//Remove references from XML File
$xml_file = 'gallery.xml';
$xml = simplexml_load_file($xml_file);

//Re-make XML --- MAKES A BETTER XML OUTPUT
$xml_out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml_out .= "<gallery>\n";
foreach($xml as $image) {
//Check every image on the XML file
for ($i = 0; $i < $how_many; $i++) {
//Check every image checked
if ($image->filename != $checked[$i]){
//Add image into the output if is not checked
$xml_out .= " <image>\n";
$xml_out .= " <filename>". $image->filename ."</filename>\n";
$xml_out .= " <caption>". $image->caption ."</caption>\n";
$xml_out .= " </image>\n";
}
}
}

$xml_out .= "</gallery>";

//Write XML
$handle = fopen($xml_file, 'w');
fwrite($handle, $xml_out);
fclose($handle);

//Results
echo $how_many.' file(s) successfully deleted.';
} else {
//No files selected
echo 'No files selected.';
}
}


Hope your XML file is something like this:



<?xml version="1.0" encoding="UTF-8"?>
<gallery>
<image>
<filename>454689.jpg</filename>
<caption>My fun image</caption>
</image>
<image>
<filename>343422_3873.jpg</filename>
<caption>Awesome vacation</caption>
</image>
<image>
<filename>9348_dfs.jpg</filename>
<caption>My family</caption>
</image>
</gallery>


Good luck.

oldnoisy
09-13-2009, 03:59 AM
Hey, thanks a ton for that suggestion! The duplicate delete was actually for deleting a copy of the image made in another folder, as you'll see below.

The code still isn't functional though, it still adds the deleted images as a reference.


Here is the full code I have now:



<?PHP

error_reporting(E_ALL);


if(!empty($_POST['submit'])) {
$checked = $_POST['checked'];
$how_many = count($checked);

if ($how_many > 0) {
//Delete image files
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/thumbs/'.$checked[$i])){
@unlink('/home/content/m/u/c/muchomungo/html/phpmanager/thumbs/'.$checked[$i]);
}
}

//Remove references from XML File
$xml_file = 'gallery.xml';
$xml = simplexml_load_file($xml_file);

//Re-make XML --- MAKES A BETTER XML OUTPUT
$xml_out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml_out .= "<simpleviewergallery maxImageWidth=\"480\" maxImageHeight=\"480\" textColor=\"0xFFFFFF\" frameColor=\"0xFFFFFF\" frameWidth=\"20\" stagePadding=\"40\" navPadding=\"40\" thumbnailColumns=\"3\" thumbnailRows=\"3\" navPosition=\"left\" vAlign=\"center\" hAlign=\"center\" title=\"Maxs Tattoos\" enableRightClickOpen=\"true\" backgroundImagePath=\"\" imagePath=\"\" thumbPath=\"\">\n";
foreach($xml as $image) {
//Check every image on the XML file
for ($i = 0; $i < $how_many; $i++) {
//Check every image checked
if ($image->filename != $checked[$i]){
//Add image into the output if is not checked
$xml_out .= " <image>\n";
$xml_out .= " <filename>". $image->filename ."</filename>\n";
$xml_out .= " <caption>". $image->caption ."</caption>\n";
$xml_out .= " </image>\n";
}
}
}

$xml_out .= "</simpleviewergallery>"."\n";

//Write XML
$handle = fopen($xml_file, 'w');
fwrite($handle, $xml_out);
fclose($handle);

//Results
echo $how_many.' file(s) successfully deleted.';
} else {
//No files selected
echo 'No files selected.';
}
}

?>

zactanaz
09-13-2009, 04:36 AM
What does the $checked array look like?

oldnoisy
09-13-2009, 04:38 AM
Fixed.

I took out the for() loop because AFAIK it already loops the XML file anyways.

Here is the final:



if(!empty($_POST['submit'])) {
$checked = $_POST['checked'];
$how_many = count($checked);
print_r($checked);
if ($how_many > 0) {
//Delete image files
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/thumbs/'.$checked[$i])){
@unlink('/home/content/m/u/c/muchomungo/html/phpmanager/thumbs/'.$checked[$i]);
}
}

//Remove references from XML File
$xml_file = 'gallery.xml';
$xml = simplexml_load_file($xml_file);

//Re-make XML --- MAKES A BETTER XML OUTPUT
$xml_out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml_out .= "<simpleviewergallery maxImageWidth=\"480\" maxImageHeight=\"480\" textColor=\"0xFFFFFF\" frameColor=\"0xFFFFFF\" frameWidth=\"20\" stagePadding=\"40\" navPadding=\"40\" thumbnailColumns=\"3\" thumbnailRows=\"3\" navPosition=\"left\" vAlign=\"center\" hAlign=\"center\" title=\"Maxs Tattoos\" enableRightClickOpen=\"true\" backgroundImagePath=\"\" imagePath=\"\" thumbPath=\"\">\n";
foreach($xml as $image) {
if (!in_array($image->filename, $checked)) {
//Add image into the output if is not checked
$xml_out .= " <image>\n";
$xml_out .= " <filename>". $image->filename ."</filename>\n";
$xml_out .= " <caption>". $image->caption ."</caption>\n";
$xml_out .= " </image>\n";
}
}

$xml_out .= "</simpleviewergallery>"."\n";

//Write XML
$handle = fopen($xml_file, 'w');
fwrite($handle, $xml_out);
fclose($handle);

//Results
echo $how_many.' file(s) successfully deleted.';
} else {
//No files selected
echo 'No files selected.';
}
}

nathanael
04-19-2010, 03:16 AM
Dear all,

i would like to know how to let user insert "$_POST[$checked] "

in previous page

could you please tell me in code please. ~

Thank you~!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Fou-Lu
04-19-2010, 06:15 AM
Dear all,

i would like to know how to let user insert "$_POST[$checked] "

in previous page

could you please tell me in code please. ~

Thank you~!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



Start a new thread instead of posting in a completely unrelated topic.
Clairify your question when you create the thread; I have no idea what you're talking about.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum