• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

[PHP] Print dynamic table

Wojtek

W as in Whisky
NLC
im printing a listing of all files in a folder using a simple script.

however I dont want it to be all in one single column, thus im making a table.

5 items <---> (width)
and as many colums as needed (5x?)

thats my code:

PHP:
<? 
$maindir = "." ;
$mydir = opendir($maindir) ; 
$exclude = array( "index.php" , ".", "..") ;
?>
<div align="center">
  <center>
  <table border="0" width="440" cellspacing="0" cellpadding="0">
    <tr>
<?
while($fn = readdir($mydir)) 
{ if ($fn == $exclude[0] || $fn == $exclude[1] || $fn == $exclude[2]) continue; 
echo "<td width=\"20%\" align=\"center\"><img src=\"$fn\"></td>"; }
closedir($mydir);
?>

    </tr>
  </table>
  </center>
</div>

I need to make it look something similar to:

PHP:
<?
while($fn = readdir($mydir)) 
{ if ($fn == $exclude[0] || $fn == $exclude[1] || $fn == $exclude[2]) continue;
(if $x = 5 then echo "</tr><td>" and $x=0); 
echo "<td width=\"20%\" align=\"center\"><img src=\"$fn\"></td>";
$x=$x+1;}
closedir($mydir);
?>

basically i need it to print </tr><td> every 5 items echo'd so it creates another line in the table. so I set a var $x and add 1 to it everytime an item is echo'd and when its at 5, make it output the </tr><td> and reset $x to 0
 
[fixed]


PHP:
<?
$x=0;
while($fn = readdir($mydir)) 
{ if ($fn == $exclude[0] || $fn == $exclude[1] || $fn == $exclude[2]) continue;
if ($x==5) {echo "</tr><tr>";}
if ($x==5) {$x=0;}
echo "<td width=\"20%\" align=\"center\"><a href=\"$fn\"><img src=\"$fn\" border=\"0\"></a></td>";
$x=$x+1;}
closedir($mydir);
?>


maybe someone will find this usefull :)
 
Back
Top