简体   繁体   中英

xml delete items in php

i have a warning in php but i don't know what it is the code is:

    <?php
    session_start();
    function clearfile(){
    $usuarios= simplexml_load_file('../carrito/cart.xml');
    $filename='../carrito/cart.xml';
    $username = $_SESSION['un'];
    $length = count($usuarios->carro);
     foreach ($usuarios->carro as $elemento){
      if($elemento->usuario==$username){
       for ($i=0; $i < $length; $i++) {
        if ($usuarios->carro[$i]->usuario==$username) {
         unset($usuarios->carro[$i]);
         break;
     }
    }
    file_put_contents($filename,$usuarios->saveXML());
    }
    }
    }
    ?>

the xml file cart.xml is

    <?xml version="1.0"?>
    <info>
    <carro>
     <id>4</id>
     <usuario>Gera</usuario>
     <producto>Keep Calm</producto> 
     <Size>M</Size>
     <cantidad>1</cantidad>
     <precio>130</precio>
    </carro>
    <carro>
     <id>5</id>
     <usuario>Gera</usuario>
     <producto>Jaws</producto>
     <Size>M</Size>
     <cantidad>1</cantidad>
     <precio>120</precio>
    </carro>
    <carro>
     <id>7</id>
     <usuario>alex</usuario>
     <producto>Gatitos</producto>
     <Size>M</Size>
     <cantidad>1</cantidad>
     <precio>78</precio>
    </carro>
    <carro>
     <id>8</id>
     <usuario>alex</usuario>
     <producto>Jaws</producto>
     <Size>M</Size>
     <cantidad>1</cantidad>
     <precio>120</precio>
    </carro>
    </info>

and the error is

   Warning: clearfile(): Node no longer exists in 
   /opt/lampp/htdocs/Hubble/compra/compra.php on line 8

the idea is to delete all the nodes of the user for example after execute the function clearfile with the username alex the output should be

    <?xml version="1.0"?>
    <info>
    <carro>
     <id>4</id>
     <usuario>Gera</usuario>
     <producto>Keep Calm</producto> 
     <Size>M</Size>
     <cantidad>1</cantidad>
     <precio>130</precio>
    </carro>
    <carro>
     <id>5</id>
     <usuario>Gera</usuario>
     <producto>Jaws</producto>
     <Size>M</Size>
     <cantidad>1</cantidad>
     <precio>120</precio>
    </carro>
    </info>

Instead of using simpleXML, try using Xpath to find parent nodes of username matching nodes.

Once Xpath returns the result, then loop through the result to go up to parent node and delete that child node to remove entire corresponding carro node.

<?php
session_start();

function clearfile() {

    $filename='../carrito/cart.xml';

    $xml = new DOMDocument();
    $xml->load($filename);
    $xpath = new Domxpath($xml);

    $username = $_SESSION['un'];
    $elements = $xpath->query("//carro/usuario[text()='".$username."']/..");

        if ($elements->length > 0) {
            foreach ($elements as $node) {
                $node->parentNode->removeChild($node);
            }
        }
    file_put_contents($filename,$xml->saveXML());
    }    

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM