Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Sep 2009
    Location
    Montreal, QC
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Removing XML node(s) by value

    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'
    Code:
    <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.
    PHP Code:


    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
    Last edited by oldnoisy; 09-13-2009 at 12:34 AM.

  • #2
    New Coder
    Join Date
    Apr 2006
    Location
    Planet Earth
    Posts
    59
    Thanks
    1
    Thanked 8 Times in 8 Posts
    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/functio...ment-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.
    PHP Code:
    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:

    Code:
    <?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.

  • Users who have thanked zactanaz for this post:

    oldnoisy (09-13-2009)

  • #3
    New Coder
    Join Date
    Sep 2009
    Location
    Montreal, QC
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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.'
        } 
    }

    ?>

  • #4
    New Coder
    Join Date
    Apr 2006
    Location
    Planet Earth
    Posts
    59
    Thanks
    1
    Thanked 8 Times in 8 Posts
    What does the $checked array look like?

  • #5
    New Coder
    Join Date
    Sep 2009
    Location
    Montreal, QC
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Fixed.

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

    Here is the final:

    PHP Code:
    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.'
        } 
    }
    Last edited by oldnoisy; 09-13-2009 at 03:50 AM.

  • #6
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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~!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by nathanael View Post
    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~!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    1. Start a new thread instead of posting in a completely unrelated topic.
    2. Clairify your question when you create the thread; I have no idea what you're talking about.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •