Jump to content

insert random pic into this script


dsjoes

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/230051-insert-random-pic-into-this-script/
Share on other sites

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>

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

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.