Good day to you all,
I was wondering how can I display the result of the array of files I have read from a directory.
I know How to read from a directory but I am wondering how to diplay the result in a table with, let's say, rows of 5 images.
What would happen if I don't have enough of file to make a row ?
Thank to you !
Display array, or dir read in rows of five ?
Moderator: General Moderators
- Peuplarchie
- Forum Contributor
- Posts: 148
- Joined: Sat Feb 04, 2006 10:49 pm
-
programmingjeff
- Forum Commoner
- Posts: 26
- Joined: Fri Jan 05, 2007 10:56 am
If you want images, replace the contents of the table cells with your image tags.
Code: Select all
<?php
$dir = '.'; // Path to your directory
echo '<table border="1"><tr>';
$col = 0;
$dr = dir($dir); // Read the directory
while($f = $dr->read()) {
if($f == '.' || $f == '..') continue; // Don't display these items
echo "<td>$f</td>";
if($col++ && !($col % 5)) echo '</tr><tr>'; // If we are at the 5th column, add a new row
}
// Finish off the blank cells for the last row
while($col++ % 5) echo '<td> </td>';
echo '</tr></table>';
?>-
programmingjeff
- Forum Commoner
- Posts: 26
- Joined: Fri Jan 05, 2007 10:56 am
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
I noticed it's a link and when clicked nothing displayed so i considered there is no such function.superdezign wrote:Hehe, dir is even a clickable link in the highlighter.miro_igov wrote:I never heard about the function dir()![]()
Maybe you forgot to define it?
EDIT: An invalid link, my apologies. GeSHi must have assumed it was a regular function.
- Peuplarchie
- Forum Contributor
- Posts: 148
- Joined: Sat Feb 04, 2006 10:49 pm
AND THE ANSWER WAS....
Code: Select all
<?php
$image_counter = 3;
$row_counter = 0;
$cell_counter = 0;
$show=array('.jpg','.JPG','.gif','.GIF');
$path = './images/';
$dir_handle = @opendir($path) or die("Unable to open $path");
$image_table = "<table>\n<tr><th colspan=5>Existing Pics in Directory:</th></tr>\n<tr>";
while (false !== ($file = readdir($dir_handle))) {
if(in_array(substr($file,-4,4),$show)){
if(!(($image_counter + 2) % 5)){
$row_counter++;
}
$image_table .=(($image_counter + 2) % 5)? "" : "</tr>\n<tr>";
$image_table .= "\n<td><img src=\"$path$file\"><br>$file</td>\n";
++$image_counter;
++$cell_counter;
}
}
$colspan= ($row_counter * 5) - $cell_counter;
$image_table .= ($cell_counter % 5) ? "<td colspan=$colspan> </td>" : "";
$image_table .= "</tr>\n</table>\n";
echo "$image_table<br><br>";
echo "<b>Total Cells w pics: $cell_counter<br>";
echo "<b>Total Rows: $row_counter<br>";
echo "<b>Empty Cells in last row: $colspan";
?>