dsjoes Posted March 9, 2011 Share Posted March 9, 2011 i have got this script which displays all folders within a folder but what i want to do now is get it to display a random picture for each of the folders that it is showing in the grid. i know how to display a random image using array_rand() but i don't know how i would implement it. <form name="Gallery" method="post"> <?php $path = "gallery_files/gallery/"; $dir_handle = @opendir($path) or die("Unable to open folder"); echo "<table cellspacing='15'>"; echo "<tr>"; while (false !== ($folder = readdir($dir_handle))) { if($folder == "pictures.php") continue; if($folder == ".") continue; if($folder == "..") continue; echo ($x % 6 == 0) ? "</tr><tr>" : ""; echo "<td><a href='pictures/?folder=$folder'><img src='path/to/random/image' style='height:auto;width:110px;' alt='$folder'></a><br /><a href='pictures/?folder=$folder'>$folder</a></td>"; $x++; } echo "</tr>"; echo "</table>"; closedir($dir_handle); ?> </form> Quote Link to comment https://forums.phpfreaks.com/topic/230051-insert-random-pic-into-this-script/ Share on other sites More sharing options...
Psycho Posted March 9, 2011 Share Posted March 9, 2011 Where are the images? Are they in the subfolders? What file extensions do they have? Quote Link to comment https://forums.phpfreaks.com/topic/230051-insert-random-pic-into-this-script/#findComment-1184892 Share on other sites More sharing options...
Psycho Posted March 9, 2011 Share Posted March 9, 2011 This will work if the images are all have the jpg extension <?php $path = "gallery_files/gallery/"; $columns = 6; $ds = DIRECTORY_SEPARATOR; //Get array of all folders $folders = glob("{$path}*", GLOB_ONLYDIR); //Create ouput for folders $folderList = ''; foreach($folders as $idx => $folder) { //Get images from folder $images = glob("{$path}{$folder}{$ds}*.[jJ][pP][gG]"); //Set random picture $image_path = $images[array_rand($images)]; //Open new row as needed if($idx%$columns == 0) { $folderList .= " <tr>\n"; } //Create TD content for folder $folderList .= " <td>"; $folderList .= "<a href='pictures/?folder=$folder'>"; $folderList .= "<img src='$image_path' style='height:auto;width:110px;' alt='$folder'>"; $folderList .= "</a>"; $folderList .= "<br /><a href='pictures/?folder=$folder'>$folder</a>"; $folderList .= "</td>\n"; //close row as needed if($idx%$columns == $columns-1) { $folderList .= " </tr>\n"; } } //Close last row if needed if($idx%$columns != $columns-1) { $folderList .= "</tr>\n"; } ?> <form name="Gallery" method="post"> <table cellspacing='15'> <?php echo $folderList; ?> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/230051-insert-random-pic-into-this-script/#findComment-1184902 Share on other sites More sharing options...
dsjoes Posted March 9, 2011 Author Share Posted March 9, 2011 all of the pictures are are .jpg but the pictures don't show up so i checked the source and the image source is empty so it just shows the links gallery_files/gallery/Group1. and how do you do it so that it just shows the folder name like Group1 like the code i provided does and the pictures/?folder= part should show pictures/?folder=Group1 instead of pictures/?folder=gallery_files/gallery/Group1. Thanks for the help yes they are in sub folders inside the galley folder Quote Link to comment https://forums.phpfreaks.com/topic/230051-insert-random-pic-into-this-script/#findComment-1184979 Share on other sites More sharing options...
Psycho Posted March 9, 2011 Share Posted March 9, 2011 Ahh, I was testing using the current folder, i.e. $path = ''; This should work <?php $path = "gallery_files/gallery/"; $columns = 6; $ds = DIRECTORY_SEPARATOR; //Get array of all folders $folders = glob("{$path}*", GLOB_ONLYDIR); //Create ouput for folders $folderList = ''; foreach($folders as $idx => $folder) { //Get images from folder $images = glob("{$folder}{$ds}*.[jJ][pP][gG]"); //Get random picture $image_path = $images[array_rand($images)]; //Open new row as needed if($idx%$columns == 0) { $folderList .= " <tr>\n"; } //Create TD content for folder $folderName = basename($folder); $folderList .= " <td>"; $folderList .= "<a href='pictures/?folder=$folder'>"; $folderList .= "<img src='$image_path' style='height:auto;width:110px;' alt='$folder'>"; $folderList .= "</a>"; $folderList .= "<br /><a href='pictures/?folder=$folderName'>$folderName</a>"; $folderList .= "</td>\n"; //close row as needed if($idx%$columns == $columns-1) { $folderList .= " </tr>\n"; } } //Close last row if needed if($idx%$columns != $columns-1) { $folderList .= "</tr>\n"; } echo "</pre>"; ?> <form name="Gallery" method="post"> <table cellspacing='15'> <?php echo $folderList; ?> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/230051-insert-random-pic-into-this-script/#findComment-1185015 Share on other sites More sharing options...
dsjoes Posted March 9, 2011 Author Share Posted March 9, 2011 thanks that works Quote Link to comment https://forums.phpfreaks.com/topic/230051-insert-random-pic-into-this-script/#findComment-1185069 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.