pindawg Posted November 19, 2008 Share Posted November 19, 2008 Ok, I'm not a developer by any means, but I thought I'd take a stab at this solution a buddy of mine needs. I've managed to make script (pieced together from a few forums.) that dynamically fills in a table 3 columns wide with every image in a directory (folder). What I need now is to be able to stop filling the table once there are 6 cells full (3 columns by 2 rows) then create a numbered breadcrumb list under the table that takes you to the next set. Make sense at all? Thanks, Pindawg Code Used below: <?php $path = "images/tees"; $path_large = "images/tees/large"; $title = 'Need formula here'; $extensions = Array('.gif','.jpg','.png'); $num_cols = 3; $img_width = '215'; $images = Array(); if(@$handle = opendir($path)) { while(false!== ($file = readdir($handle))) { if($file!= '.' && $file!= '..' &&!is_dir($file) && in_array(substr($file,-4),$extensions)) { $images[] = $file; } } } echo "<table>\n"; echo "<colgroup>\n"; for($i=0;$i<$num_cols;$i++) { echo "<col width='".$img_width."' />\n"; } echo "</colgroup>\n<tr>\n"; if(count($images)>0) { $i=1; foreach($images as $image) { if($i==$num_cols+1) { echo "</tr>\n<tr>\n"; $i=1; } echo "<td><a href='$path_large/$image' rel='lightbox' title='$title'><img src='$path/$image' width='$img_width' /></a></td>\n"; $i++; } if($i <= $num_cols) { while($i<=$num_cols) { echo "<td> </td>\n"; $i++; } } } else { echo "<td>No images in specified folder</td>\n"; } echo "</tr>\n</table>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/133367-solved-php-table-help/ Share on other sites More sharing options...
vicodin Posted November 19, 2008 Share Posted November 19, 2008 Google "php pagination" and you will get your answer... unfortunately its not really easy to implement if you dont know php. Quote Link to comment https://forums.phpfreaks.com/topic/133367-solved-php-table-help/#findComment-693716 Share on other sites More sharing options...
wildteen88 Posted November 19, 2008 Share Posted November 19, 2008 To archive what you're tying to do only requires a few small changes to your code. First add // split the $images array into separate arrays containing 6 images // see http://php.net/array-chunk for documentation $pages = array_chunk($images, 6); // get the request page $page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page']-1 : 0; After the following while(false!== ($file = readdir($handle))) { if($file!= '.' && $file!= '..' &&!is_dir($file) && in_array(substr($file,-4),$extensions)) { $images[] = $file; } } } Next change if(count($images)>0) { $i=1; foreach($images as $image) { to if(count($pages[$page]) > 0) { $i=1; foreach($pages[$page] as $image) Now to add your page links change echo "</tr>\n</table>\n"; to echo "</tr>\n<tr>\n<td colspan=\"3\" align=\"center\">Page: "; for($i = 0, $p = count($pages); $i < $p; $i++) echo ' <a href="?page='.($i+1).'">'.($i+1) .'</a>'; echo "</td>\n</tr>\n</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/133367-solved-php-table-help/#findComment-693720 Share on other sites More sharing options...
pindawg Posted November 20, 2008 Author Share Posted November 20, 2008 Thank you so much for your help guys. I'll give it a shot. Quote Link to comment https://forums.phpfreaks.com/topic/133367-solved-php-table-help/#findComment-694440 Share on other sites More sharing options...
pindawg Posted November 20, 2008 Author Share Posted November 20, 2008 Ok so I now get better results using the above code. It adds pages links to the bottom of the table, but it only loads every 6th image, insted of loading 6 images into the table. Did I miss something? <?php $path = "images"; $path_large = "images/large"; $title = 'Need formula here'; $extensions = Array('.gif','.jpg','.png'); $num_cols = 3; $img_width = '215'; $images = Array(); if(@$handle = opendir($path)) { while(false!== ($file = readdir($handle))) { if($file!= '.' && $file!= '..' &&!is_dir($file) && in_array(substr($file,-4),$extensions)) { $images[] = $file; } } // split the $images array into separate arrays containing 6 images // see http://php.net/array-chunk for documentation $pages = array_chunk($images, 6); // get the request page $page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page']-1 : 0; echo "<table>\n"; echo "<colgroup>\n"; for($i=0;$i<$num_cols;$i++) { echo "<col width='".$img_width."' />\n"; } echo "</colgroup>\n<tr>\n"; if(count($pages[$page]) > 0) { $i=1; foreach($pages[$page] as $image) if($i==$num_cols+1) { echo "</tr>\n<tr>\n"; $i=1; } echo "<td><a href='$path_large/$image' rel='lightbox' title='$title'><img src='$path/$image' width='$img_width' /></a></td>\n"; $i++; } if($i <= $num_cols) { while($i<=$num_cols) { echo "<td> </td>\n"; $i++; } } } else { echo "<td>No images in specified folder</td>\n"; } echo "</tr>\n<tr>\n<td colspan=\"3\" align=\"right\">Page: "; for($i = 0, $p = count($pages); $i < $p; $i++) echo ' <a href="?page='.($i+1).'">'.($i+1) .'</a>'; echo "</td>\n</tr>\n</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/133367-solved-php-table-help/#findComment-694500 Share on other sites More sharing options...
pindawg Posted November 20, 2008 Author Share Posted November 20, 2008 Ok I got it to work now. Turns out I was missing an open tag.. DUH!!! Thank you so much wildteen88 Quote Link to comment https://forums.phpfreaks.com/topic/133367-solved-php-table-help/#findComment-694516 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.