...

View Full Version : echo <tr> every 2nd time through loop



Len Whistler
01-28-2007, 03:10 AM
How can I echo the <tr> for a new row every 2nd time through the if loop? I have tried to add a for loop using ($i = 1; $i <=$columns; $i++) but couldn't figure it out. I want the table to be 2 columns wide.


<?php

$columns = 2;

echo "<table border=\"2\">";
if ($handle = opendir('images/')) {

while (false !== ($file = readdir($handle))) {

if ($file != "." && $file != "..") {

echo "<td><img src=\"images/$file\"></td>";

echo "<tr>";

}
}

closedir($handle);
}

echo "</table>";

?>

Erockrm1
01-28-2007, 06:59 AM
<?php

$columns = 2;
$count = 0;

echo "<table border=\"2\">";
if ($handle = opendir('images/')) {

while (false !== ($file = readdir($handle))) {

if ($file != "." && $file != "..") {

echo "<td><img src=\"images/$file\"></td>";

$count++;

if ($count == 2)
{
echo "</tr><tr>";
$count = 0;
}

}
}

closedir($handle);
}

echo "</table>";

?>

I highlighted the areas I modified, I'm pretty sure this is what you wanted, if not you should have the idea now :D

Len Whistler
01-28-2007, 07:22 AM
Thanks Erockrm1, It works perfectly :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum