Here's what I did to achieve this:
<?php
function lightbox_display($dir_to_search, $rel){
$image_dir = $dir_to_search;
$dir_to_search = scandir($dir_to_search);
$image_exts = array('gif', 'jpg', 'jpeg', 'png');
$excluded_filename = '_t';
$piccounter = 0; /* this is how we monitor the length of the rows */
foreach ($dir_to_search as $image_file){
$dot = strrpos($image_file, '.');
$filename = substr($image_file, 0, $dot);
$filetype = substr($image_file, $dot+1);
$thumbnail_file = strrpos($filename, $excluded_filename);
if ((!$thumbnail_file) and array_search($filetype, $image_exts) !== false){
/*Variable $piccounter is the number of thumbnails across in the page per row*/
if ($piccounter >= 6){ echo "<tr />"; $piccounter=0; } /*Every 6, drop in a </tr> and reset the variable for the new row */
echo "<td><a href='".$image_dir.$image_file."' title='$filename' rel='".$rel."' data-lightbox=".$filename.">
<img src='".$image_dir.$filename."_t.".$filetype."' alt='".$filename."' width='100' height='80' title=''/ ><br>
$filename<br/>
</a></td>"; /*$filename is both the image name AND the caption in expanded lightbox*/
$piccounter++; /*After each image, add 1 to the $piccount counter to keep track of the length of the rows*/
}
}
}
?>
Variable $piccounter was created to keep track of how many thumbnails had been printed. When it hits 6 (or whatever number) it inserts a table row. Otherwise the foreach process creates TD's.
On the page itself I add the table open/close around this script call.