Is it possible for images to be randomised like this please?

If you have 10 images in one folder and 10 images in another, then on a certain website page, the images from one folder would appear in a vertical way in a random order, then if browsing away from that page or on refreshing, those 10 images would be replaced with the 10 from the other folder, again in a random order. Is this possible only using PHP please ?

Any help much appreciated.

Dez

Yes… I can’t supply code because I don’t know your image names and file paths etc but basically you need 2 arrays with 10 image names in each OR you need to scan the 2 directories and dynamically get the filenames.

Once you’ve done that you just need to use rand() to select random images from the array.

Something along these lines would work although it’s not been tested and you’d need to edit bits like the directory names…

<?php

session_start();

$folder1 = "/first_folder";
$folder2 = "/second_folder";

$to_use = $folder1;
if( isset( $_SESSION["show_second"] ) )
	$to_use = $folder2;
else
	$_SESSION["show_second"] = true;

$files = scandir( $to_use );
shuffle( $files );

echo "<ul>";
foreach( $files as $file )
{
	if( $file != "." && $file != ".." )
		echo "<li><img src=\\"". $file ."\\" />";
}
echo "</ul>";

?>

Then you can use CSS to style the list to get the images vertically aligned

Many thanks tangoforce and Martyn for the quick help, it’s really appreciated. A couple of things if you can - no javascript needed for this ? Also, within the folders are index.hmtl files, is there any of this just doing gif, jpg png files in the folders please? Lastly, will the images display on page load in a random order? Can’t see rand anywhere?

No Javascipt.

For building the array check out Glob

See Shuffle

Also, the images are hyperlinked, so how would you get in all the hrefs, alts and titles etc please ?

The helps appreciated.

Dez

Thanks Rubble, that’s the javascript and random questions done :slight_smile: Just need to get the other questions sorted.

I read through the glob page, is this to help with the html files problem please?

Yes glob will sort your php problem if used like:


$files = glob($to_use."{*.jpg, *png, *.gif}", GLOB_BRACE);

The paths might be different to the ones posted by martynmj

This will add the image size, hyperlink and an alt tag of the image path


$size = getimagesize( $file );

echo "<li><a href=\\"". $file ."\\" ><img src=\\"". $file ."\\" ".$size[3]." alt=\\"". $file ."\\" /></a>"; 

Thanks Rubble, how will it know what alts, hrefs to add? Also can it add the title tag as well please?

I should also add, that the hrefs, alts and titles for each image would be different.

The helps much appreciated.

Dez

What I do is have a text file with the same name as the image containing the alt text. You just need to change the extension of the image from say .jpg to .txt

If the images you are displaying on the page are thumbnails then I would either put the thumbnails in one folder and the larg images in another both with the same name and just change the folder name on the call to the image. OR add something like th_ to the image and do not display any images with th_ in the fromt of the name.
So I would have:
image.jpg - full size image
th_image.jpg - thumbnail image
image.txt - alt tags

or ( probably the easiest method )

Thumbs folder with image.jpg in
Normal folder with image.jpg and image.txt in

This is basicaly what I do for this gallery: Gallery

You could use a 2 dimensional array to store all the attributes you need for each image like image path, alt, href and anything else you need. Then as you loop through the array rows to generate the html to display the images, get the attribute you need at any point in the html from the appropriate column in the current array row.

This means that the array will need to be modified everytime an image is changed. With the other method if an image is changed or another uploaded the code will not need to be touched.

Basicaly Dez needs to decide wether the images are going to stay for a while and what directory system etc. he wants.

Thanks Lazzarus, how would that work in practice please? Would the 2 dimensional array be on the same page?

Thanks Rubble, the images are as they are, no thumbnails.

The images would usually be around for 1/2 months and what are the choices in directory system please?

I have not tested the session part.

Create a folder > create two folders inside the folder named “first_folder” and “second_folder” > put 10 images in each folder > put 10 text files in each folder with the same names as the images BUT change the extension to .txt > Save the code below as index.php in the first folder


<?php
session_start();

// Folders containing images - 10 in each including the text files
$folder1 = "first_folder/";
$folder2 = "second_folder/";

// This part is untested
$to_use = $folder1;
if( isset( $_SESSION["show_second"] ) )
    $to_use = $folder2;
else
    $_SESSION["show_second"] = true;

// Read all the jpg, png and gif files into an array
$files = glob($to_use."{*.jpg,*.png,*.gif}", GLOB_BRACE);

// Randomise the array 
shuffle( $files );

// Start the list
echo "<ul>";
foreach( $files as $file )
{

// Get the size of the image
$size = getimagesize( $file );
 
// Change the file extension for the al text 
$alt_file = preg_replace('"\\.(jpg|gif|png)$"', '.txt', $file); 

// Read the text file into the alt variable
$alt = file_get_contents( $alt_file );
 
// Display the image 
echo "<li><a href=\\"". $file ."\\" ><img src=\\"". $file ."\\" ".$size[3]." alt=\\"". $alt ."\\" /></a>"; 

}

// End the list
echo "</ul>";

?>

Thanks nomadd, but trying to avoid the cms route and don’t have a database.

Many thanks for your time and effort Rubble, it’s much appreciated. Just wondering, do you (or anyone else) feel that PHP includes would somehow be ok for this, that way, you could just have two php files, and on page load, one page is included and on another page load the other page is included?