Skip to content
Advertisement

I want to delete data in xml but it can’t

When I try to delete, I can’t delete it properly. It will stuck at number 2

if(isset($_GET['action'])) {
    $mahasiswaa = simplexml_load_file('input.xml');
    $nim = $_GET['nim'];
    $index = 0;
    $i =1;
    $i++;
    foreach($mahasiswaa->mahasiswa as $mahasiswa){
        if($mahasiswa['nim']==$nim){
            $index = $i;
            break;
        }
    }
    unset($mahasiswaa->mahasiswa[$index]);
    file_put_contents('input.xml', $mahasiswaa->asXML());
}

Advertisement

Answer

your $i variable is constantly 2, the i++ should probably be inside the loop?

also, if your condition is never met, $index is 0 and its use will likely lead to an error.

The code will act only on the first match, whats ok, if that is what you expect.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement