Page 1 of 1

directory list using template to html

Posted: Thu Mar 11, 2004 7:22 pm
by wesnoel
OK I need to write directory contents to an html file using a template.

I know how to write to a text file, but how do I use a template so it is in html.

This is what I'm using to output the directory list on the fly.

Code: Select all

<?php 


if ($handle = opendir('.')) { 
   while (false !== ($file = readdir($handle))) { 
	if($file == '.' || $file == '..' || $file == 'images')
	
	{
	    continue;
	}
	
	if(is_dir($file))
	
	{



//display template


$name = strtoupper($file);
$thumb = strtolower($file);

include "template.php";

echo "<br>";
	   
	 

	 
  } 
    
   }  
   closedir($handle); 
   
   
} 

?>
And this is the template file...

Code: Select all

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;link href="template.css" rel="stylesheet" type="text/css"&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;table width="415" height="155" border="0" class="cell"&gt;
  &lt;tr&gt; 
    &lt;td width="36%" height="149" align="left" valign="middle"&gt; 
      &lt;div align="center"&gt; &lt;?php echo "&lt;img src="images/thumbs/$thumb.jpg"&gt;"; ?&gt; &lt;br&gt;
        &lt;font color="#333333"&gt;&lt;strong&gt; &lt;?php echo $name ; ?&gt; &lt;/strong&gt;&lt;/font&gt;&lt;/div&gt;&lt;/td&gt;
    &lt;td width="64%"&gt;&lt;div align="center"&gt; 
        &lt;table width="75%" height="51" border="0" Class="dash"&gt;
          &lt;tr&gt; 
            &lt;td height="20" colspan="4"&gt; &lt;div align="center"&gt;&lt;/div&gt;
              &lt;div align="center"&gt; 
                &lt;p&gt;&lt;font color="#666666" size="-1" face="arial"&gt;&lt;strong&gt;Download 
                  ZIP Files Below&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;
              &lt;/div&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt; 
            &lt;td width="9%"&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;/td&gt;
            &lt;td width="41%"&gt;&lt;div align="center"&gt;&lt;font size="-1" face="arial"&gt;&lt;? echo "&lt;a href="$file/video"&gt;Videos&lt;/a&gt;" ?&gt;&lt;/font&gt;&lt;/div&gt;&lt;/td&gt;
            &lt;td width="41%"&gt;&lt;div align="center"&gt;&lt;font size="-1" face="arial"&gt;&lt;? echo "&lt;a href="$file/pics"&gt;Photos&lt;/a&gt;" ?&gt;&lt;/font&gt;&lt;/div&gt;&lt;/td&gt;
            &lt;td width="9%"&gt;&amp;nbsp;&lt;/td&gt;
          &lt;/tr&gt;
        &lt;/table&gt;
      &lt;/div&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
How can I merge the two and make an html file to ease server load.
Any help would be great!

TIA,

Wes/

Re: directory list using template to html

Posted: Thu Mar 11, 2004 10:14 pm
by TheBentinel.com
I could be wrong (and probably am, check my other posts!) but it looks like you're thinking backwards on this. You don't want to include the template into your PHP, you want to include the PHP in your template. Something on the order of:

Code: Select all

<html>
<table>
<?php
  for each (thing in MyGroup)
  {
    print "<tr>";
    print "<td>" . thing.column1 . "</td>";
    print "<td>" . thing.column2 . "</td>";
    print "<tr>";
  }
?>
</table>
</html>
See what I mean? You want the markup surrounding your PHP code, not the other way around.

If you want to keep the HTML in a separate file, then you'll probably need two templates. One would be everything above the PHP code, the other would be everything below. Then the PHP script can include them.