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
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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.