View Full Version : Randomizing Images Using PHP
Archmage
04-08-2010, 10:19 AM
Hello all,
I am somewhat of a novice and have never used PHP before. I am busy creating a site and am wanting to allow a vast number of images within different folders appear vertically on the page, and to randomize everytime the page is refreshed. The image should also be linkable.
Could anybody please help me on this matter? I would greatly appreciate it!:confused:
djr33
04-08-2010, 01:41 PM
If you want specific help, post specific information (a lot more detail than that and hopefully a link).
In general, though, this should mean creating an array of images. Then you can randomize the array:
$array = (IMAGES);
shuffle($array);
foreach ($array as $img) {
echo $img;
}
That's a rough outline and you can of course make it more complex, echo an <a> link tag, etc.
Archmage
04-09-2010, 06:51 AM
Hmmmm... as I say, complete novice to PHP, and JavaScript actually, but I will try and expand as much as possible. As yet I have no link. Right, so here goes:
I have a personal website that I am creating. It is a three column website, all CSS created. In the right hand column I want to allow randomized images from a directory to appear in the column vertically. I don't want them timed though, they must only randomize when the page is refreshed. The actual images though need to be linked to a child page for easy navigation.
I hope this explains it a little better?
djr33
04-09-2010, 03:25 PM
The basic structure above will work.
To setup the array, you can loop through a folder and grab all images. There are a few ways to do this. See opendir() on php.net for an example. Then just use readdir() in a while loop and IGNORE any non-image files (and especially any '.' or '..' files-- these are pseudo-directories, the first is the current directory and the second is the one above it).
Shuffle the array.
Then when you loop through the images (from the array) just echo them as you'd like, including links or whatever you want. That's the easy part.
This isn't a 'standard' script or anything-- you need to create it by knowing how the parts work. PHP is a tool that can design pretty much anything you want-- you just have to know the individual parts.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.