Need this to REPLACE not APPEND

The following block of code is 100% working. The problem is that every time I refresh the page it just adds on to what it had previously wrote. This just makes it repeat the information in the container div. I tried to use replaceChild instead of appendChild but all attempts to adjust the code results in failure. I would be grateful if someone could help me make this code replace the content that is between the container div tags instead of adding on to it.

<?php
	$file = "./test.html";
    $imgs = glob("1/*.*");
    $dom = new DOMDocument();
    $dom->loadHTMLFile($file);
    $container = $dom->getElementByID("container");
    
    if(count($imgs) == 0 || $imgs === false)    die('Error: glob is failing.');
    foreach($imgs as $img)
    {
        $basename = pathinfo($img, PATHINFO_BASENAME);
        $tag = $dom->createElement("img");
        $tag->setAttribute('src', '1/'.$basename);
        $container->appendChild($tag);
        $tag = null;
    }

    $dom->saveHTMLFile($file);
    ?>

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>

    <div id="container"></div>

</body>
</html>

The library you are using is not change friendly. I personally use SimpleXML for things like this.

You could remove the node then add it back, but that is a poor work around. I’m not exactly sure what the goal of the function is, so I can’t be of much more help.

I have a slideshow that is very specific in the way it wants to operate. the images are picked up by the javascript that is searching for divs that are in line. I just want the php to write to an html file replacing what it had already written the time before so that the javascript will see it how it is suppose to. I have found no better way of doing this. I know there is a way with php. If you run the code that I provided and refresh the page a few times you will see what the issue is.

So I did it. I answered my own dilemma. I want to say thank you to everyone who has answered back and I really like this side and the help that is provided from a vast community. I thought I would share how I did it. PHP reads top to bottom as we all know so I used 2 different blocks of code. The first one deletes the content between the content div and the other populates it. Here is my complete code and once again thank you. :slight_smile:

<?php
$html = new DOMDocument(); 
$html->loadHTMLFile('test.html'); 
$html->getElementById('container')->nodeValue = '';
$html->saveHTMLFile("test.html");
?>
 <?php
	$file = "./test.html";
    $imgs = glob("1/*.*");
    $dom = new DOMDocument();
    $dom->loadHTMLFile($file);
    $container = $dom->getElementByID("container");
    
    if(count($imgs) == 0 || $imgs === false)    die('Error: glob is failing.');
    foreach($imgs as $img)
    {
        $basename = pathinfo($img, PATHINFO_BASENAME);
        $tag = $dom->createElement("img");
        $tag->setAttribute('src', '1/'.$basename);
        $container->appendChild($tag);
        $tag = null;
    }

    $dom->saveHTMLFile($file);
    ?>

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>

    <div id="container"></div>

</body>
</html>
Sponsor our Newsletter | Privacy Policy | Terms of Service