Hi. Welcome to TSDN!
First of all, I would recommend putting the images you want to be shown in another folder, separate from the files you do not want shown.
This would eliminate the need to filter out the files you don't want to show.
Also, you may want to check out the
glob function to replace the readdir function. The glob function is much easier to work with and it gives you the option to filter the search results by a pattern.
And, different from the readdir function, the glob function returns all it finds as an array, so it can be sorted and/or modified as you want.
For example, if you want to list all files in a specific directory:
-
# The * in the glob pattern is used as a wildcard.
-
$fileList = glob("path/to/dir/*.*")
-
-
# Print each file
-
echo "Files found:";
-
foreach($fileList as $file) {
-
echo " - ". $file;
-
}
-
Or, if you only want to see jpg or png images:
-
# Create pattern
-
$globPattern = "{*.jpg, *.jpeg, *.png}";
-
-
# Get a list of files
-
$fileList = glob($globPattern, GLOB_BRACE);
-
-
# Print each file
-
echo "Files found:";
-
foreach($fileList as $file) {
-
echo " - ". $file;
-
}
-
And, finally, getting to your actual question :P
You can use the modulus operator (%) to divide your table into four columns.
Like so:
-
echo "<table><tr>";
-
# Loop through 15 times, each time incrementing $i by one.
-
for($i = 0; $i < 15; $i++)
-
{
-
# Create a new row every four columns
-
if($i % 4 == 0 and $i != 0)
-
{
-
echo "</tr><tr>";
-
}
-
-
# Add a column
-
echo "<td>Column nr: $i</td>";
-
}
-
echo "</tr></table>";
-
The modulus operator essentially divides the first number with the second number and returns the leftovers.
Another way to think of it is; the second number is subtracted from the first number until the first number is less then the second number, then what is left of the first number is returned.
So, by using how many columns have already been added as the first number, and 4 as the second number, the number returned will always be 0 every 4 rows.