Display array, or dir read in rows of five ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Display array, or dir read in rows of five ?

Post 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 !
programmingjeff
Forum Commoner
Posts: 26
Joined: Fri Jan 05, 2007 10:56 am

Post 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>&nbsp;</td>';

echo '</tr></table>';
?>
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

I never heard about the function dir() :) :) Maybe you forgot to define it?
programmingjeff
Forum Commoner
Posts: 26
Joined: Fri Jan 05, 2007 10:56 am

Post by programmingjeff »

User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post 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.
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Post 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>&nbsp;</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";
?>
Post Reply