Page 1 of 1
Display array, or dir read in rows of five ?
Posted: Tue Jul 24, 2007 9:14 pm
by Peuplarchie
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 !
Posted: Tue Jul 24, 2007 10:41 pm
by programmingjeff
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>';
?>
Posted: Wed Jul 25, 2007 3:26 am
by miro_igov
I never heard about the function dir()

Maybe you forgot to define it?
Posted: Wed Jul 25, 2007 8:55 am
by programmingjeff
Posted: Wed Jul 25, 2007 9:07 am
by superdezign
miro_igov wrote:I never heard about the function dir()

Maybe you forgot to define it?
Hehe, dir is even a clickable link in the highlighter.
EDIT: An invalid link, my apologies. GeSHi must have assumed it was a regular function.
Posted: Wed Jul 25, 2007 10:33 am
by miro_igov
superdezign wrote:miro_igov wrote:I never heard about the function dir()

Maybe you forgot to define it?
Hehe, dir is even a clickable link in the highlighter.
EDIT: An invalid link, my apologies. GeSHi must have assumed it was a regular function.
I noticed it's a link and when clicked nothing displayed so i considered there is no such function.
Posted: Wed Jul 25, 2007 5:46 pm
by Peuplarchie
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";
?>