itzjmoco Posted December 8, 2012 Share Posted December 8, 2012 Ok so I have a php script that pulls all images from my "images" directory then loads them into an array an then displays them. Everything works great but i want to display just the image name and extension under each image. EX: myimage.png I have tried {$images[$i]} but it displays images/myimage.png. here is my code <table><tr> <?php //The directory to your images folder, with trailing slash $dir = "images/"; $columns = 5; //Set the extensions you want to load, seperate by a comma. $extensions = "jpeg,jpg,png,gif,bmp"; //Set the number of images you want to display per page $imagesPerPage = 5; //Set the $page variable if(!isset($_GET['page'])){ $page = 1; }else{ $page = $_GET['page']; } //Load all images into an array $images = glob($dir."*.{".$extensions."}", GLOB_BRACE); //Count the number of images $totalImages = count($images); //Get the total pages $totalPages = ceil($totalImages / $imagesPerPage); //Make sure the page you are on is not greater then the total pages available. if($page > $totalPages){ //Set the currnet page to the total pages. $page = $totalPages; } //Now find where to start the loading from $from = ($page * $imagesPerPage) - $imagesPerPage; //Now start looping $act = 0; for($i = $from; $i < ($from + $imagesPerPage); $i++){ //We need to make sure that its within the range of totalImages. if($i < $totalImages){ //Now we can display the image! ++$act; if ($act > $columns) { echo "</tr><tr> <td><img width='120px' height='80px' src='{$images[$i]}' alt='{$images[$i]}' style='border: 1px solid #bababa;'/></td>"; $act = 1; } else { echo "<td><img width='120px' height='80px' src='{$images[$i]}' alt='{$images[$i]}' style='border: 1px solid #bababa;'/></td>"; } } } echo "</table>"; //Now to display the page numbers! for($p = 1; $p <= $totalPages; $p++){ if($p == $page){ $tmp_pages[] = "<strong>{$p}</strong>"; }else{ $tmp_pages[] = "<a href='?page={$p}'>{$p}</a>"; } } //Now display pages, seperated by a hyphon. echo "<br />" . implode(" - ", $tmp_pages); ?> Quote Link to comment https://forums.phpfreaks.com/topic/271740-displaying-image-names-with-my-php-gallery/ Share on other sites More sharing options...
itzjmoco Posted December 8, 2012 Author Share Posted December 8, 2012 (edited) I am not sure if its possible to do something like this again $images = glob($dir."*.{".$extensions."}", GLOB_BRACE); $names = SOME_CODE_HERE and then do something like this /Now we can display the image! ++$act; if ($act > $columns) { echo "</tr><tr> <td><img width='120px' height='80px' src='{$images[$i]}' alt='{$images[$i]}' style='border: 1px solid #bababa;'/><br>{$names[$i]}</td>"; $act = 1; } else { echo "<td><img width='120px' height='80px' src='{$images[$i]}' alt='{$images[$i]}' style='border: 1px solid #bababa;'/><br>{$names[$i]}</td>"; } Edited December 8, 2012 by itzjmoco Quote Link to comment https://forums.phpfreaks.com/topic/271740-displaying-image-names-with-my-php-gallery/#findComment-1398192 Share on other sites More sharing options...
Christian F. Posted December 8, 2012 Share Posted December 8, 2012 I think you'll want to have a look at basename (). Quote Link to comment https://forums.phpfreaks.com/topic/271740-displaying-image-names-with-my-php-gallery/#findComment-1398206 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.