Good! Nice to see you tried it and got that close! Let me introduce you a simple example:
Code: Select all
<?php
$foo = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$colNum = 4; // Set the number of columns
$count = 1; // Initialize the counter
echo "<table border="0">\n<tr>\n"; // start the table and the first row
foreach ($foo as $value) { // Loop in the array
echo "\t<td>$value</td>\n"; // Print the data
// Now here is the point! Each 4 times ($count % $colNum == 0 (this 4 is specified by $colNum))
// we want PHP to start a new row (but only if it has not reached the array's end yet ($count != count($foo))):
if ($count % $colNum == 0 && $count != count($foo))
echo "</tr>\n<tr>\n"; // Then close the last and open a new one
$count++;
}
echo "</tr>\n</table>";
?>
See the output:
Code: Select all
<table border="0">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
</table>
Ok! Now, don't go further my post until you try to solve your script again, based on the script I post above. Below is your script with modifications.
The
% operator is a very nice trick. Take alook to the
Operators chapter of the PHP Manual
Your code:
Code: Select all
<?php
$dir = '.';//$_COOKIE[user]; // changed to run on my server...
$dh = opendir($dir); // open it for reading
echo "<div align="center"><table border="0"><tr>"; // Start printing the table (notice the <div> tag, since <center> is deprecated)
$colNum = 4; // Set the number of columns
$counter = 1; // Initialize the counter
while (($file = readdir($dh)) !== false) { // loop files
if (($file != ".") and ($file != "..") and ($file != "_vti_cnf")) {
echo "<td><p><a href="http://www.slices.net/$dir/$file"><img height="50" width="50" border="0" src="".$dir."/".$file.""></p><center><b>$file</b></center></td>\n";
if ($counter % $colNum == 0) // Each 4 time this condition is true (you can change the value of $colNum to change it)
echo "</tr><tr>\n"; // then we start a new row
$counter++; // and keep incrementing the counter
}
}
echo "</tr></table></div>"; // then close the tr, the table and the div.
?>
Have a nice learning!
Regards,
Scorphus.