Hi, I am just starting with php and i have been trying to get these two pieces of code to act right. Like they are shown everything works just fine. What i need this to do is find a certain html code on a page then write to the next line more html.
the goal is to pull the img file names from a folder then print code for each img so it will display on that page. I know it seems a bit redundant but i do have a perpose, lol. Help with this little project i would be much thankful for.

<?php

   $files = glob("1/*.*");

   for ($i=1; $i<count($files); $i++)

  {

  $image = $files[$i];

  print $image ."<br />";
  echo '<div><img u="image" src="'.$image .'" width="720px" height="480px" alt="Actual Size" /></div>'."<br /><br />";
  echo '<div><img u="thumb" src="'.$image .'" width="99px" height="66px" alt="Thumb" /></div>'."<br /><br />";
   }

 {

   $file = fopen("test.html","w");
 echo fwrite($file,"Hello World. Testing!".$i."<br>\r\n");
 fclose($file);
   }
 ?>

You could use the DOMDocument class:

An example:

<?php

$file = "./test.html";
$imgs = glob('../images/*.jpg');

$dom = new DOMDocument();
$dom->loadHTMLFile($file);
$container = $dom->getElementByID("container");

foreach($imgs as $img)
{
    $basename = pathinfo($img, PATHINFO_BASENAME);
    $tag = $dom->createElement("img");
    $tag->setAttribute('src', '/images/'.$basename);
    $container->appendChild($tag);

    $tag = null;
}

$dom->saveHTMLFile($file);

By using the method getElementByID() you can define a container in your HTML in which append each new element, so the base for test.html of the above example is:

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

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

</body>
</html>

Note: the doctype is important, otherwise it will add HTML 4.0.

ok so this is kinda wierd. I understand what the php is supose to be doing but why is it producing the meta tag eliments instead of producing something within the container?

<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Test</title></head><body>

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

</body></html>

The meta tag is added automatically, just specify your own flavour to avoid a change of charset, for example:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Test</title>
</head>
<body>

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

</body>
</html>

The fact that the tags are not added to the container means that glob() return an empty array or false. Before the loop you can add this check:

if(count($imgs) == 0 || $imgs === false)
    die('Error: glob is failing.');

Complete example:

<?php

$file = "./test.html";
$imgs = glob('../images/*.jpg');

$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', '/images/'.$basename);
    $container->appendChild($tag);

    $tag = null;
}

$dom->saveHTMLFile($file);

This works great. Thank you. Is there any way to get it to delete what is in the container before it writes to the container? for maybe in the instance that you were to remove an image or two.

It depends on what you want to do, you could remove the container and add it again, just place another node to define in which area of the page you want to add the new empty container, in this case the html page will be:

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

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

</body>
</html>

The new script is this:

<?php

$file = "./test.html";
$imgs = glob('../images/*.jpg');

$dom = new DOMDocument();
$dom->preserveWhiteSpace = true;
$dom->formatOutput = true;
$dom->loadHTMLFile($file);

$container = $dom->getElementByID("container");

if($container != null && $container->hasChildNodes())
{
    # remove the container
    $parent = $container->parentNode;
    $parent->removeChild($container);

    # create new container
    $container = $dom->createElement("div");
    $container->setAttribute("id", "container");

    # insert the new container before a specific node
    $marker = $dom->getElementByID("marker");
    $parent->insertBefore($container, $marker);
}

if(count($imgs) == 0 || $imgs === false)
    die('Error: glob is failing.');

# populate the new container
foreach($imgs as $img)
{
    $basename = pathinfo($img, PATHINFO_BASENAME);
    $tag = $dom->createElement("img");
    $tag->setAttribute('src', '/images/'.$basename);
    $container->appendChild($tag);

    # appending new lines and tabs
    $br = $dom->createTextNode("\n\t");
    $container->appendChild($br);

    $tag = null;
    $br  = null;
}

$dom->saveHTMLFile($file);

This is the easy method, but if you want, you can compare each child node values with the glob array to perform only specific operations, for this check:

Bye!

$tag = $dom->createElement("img");

Cant i use something like this to destroy the previous output then the above line would recreate it. Something like $tag = $dom->destroyElement("img");

the way the code is right now it writes the same thing when the page loads so it without deleting what is preixisting inside the div thus repeating the list of images

Applying this code at each page load is not really efficient, it can be useful to create static html files to use as cache but not for live content.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.