Here is a little trick for randomizing banners on your website. A bit quick and dirty. It needs to be on a PHP page to work.
First off we need to setup a folder on your server where the pictures will go. We need to name the pictures in order, for example, mypicture1.jpg, mypicture2.jpg and mypicture3.jpg and so on.
The next step is to edit your banner code on your website to reflect your new image files location. If you had…
<img src="//www.yoursite.com/random_images_folder_path/mypicture.jpg" />
It would turn into…
<img src="//www.yoursite.com/random_images_folder_path/mypicture<?php
echo(mt_rand(1,3)); ?>.jpg">
MT_Rand is a PHP function that randomizes from a first number to a number, in our example 1 to 3. You can have it as high as you have pictures (since you can’t randomize what you don’t have).
You could also put this image in a PHP file, perhaps called mypicture.php, and include it into your banner area. That way you only have to change it in one place.
So open a new txt file and input the above random image code. Save the text file as a .php extension to your server. Then in your picture area include this code below.
<?php include("http://www.yoursite.com/mypicture.php"); ?>
You also don’t have to use absolute paths. I did this for brevity and simplicity sake.
Well that all there is to that. Hope this helps someone.
This post was authored by Chris St. Amand. Chris has been freelance web designer and developer for the past 8 years.
Being a PHP ‘noob’ this was really useful for me, thanks!
Chris
Nice and simple – I have wanted to show a page with around 100 images on it with the pictures in a random order before, but never worked it out. The problem is that I don’t want any pictures to show more than once.
I thought about loading the pictures into a database, then iterating through them to make sure they are in a random order, but still unique, but it seems a complex way of doing something relatively simple.
Any ideas?
@Rich: Are you talking about a slideshow-type presentation?
This technique is just randomizing the image when the page is loaded, and would only change on a page reload. If you wanted to make sure they didn’t receive the same random image using this technique, you would need to keep track of the images they have seen in a cookie, check your random image against that list, and then generate a new one if there was a conflict. Also, remember to clear the cookie once it has 99 image references in it.
I’m no expert on this though, obviously, that might be a dumb way to do it. It’s just the first one I thought of =)
I’m happy that people found it helpful. This is possibly just the first article, in a line of articles on this topic. Who knows :)
MT_Rand is supposedly a better function than just RAND. I’ve read that RAND will often show you the same image again even if you have 100 images. But MT_Rand is truly random.
http://us.php.net/rand
http://us.php.net/manual/en/function.mt-rand.php
Neat trick
@Rich : i don’t know your overall knowledge of PHP but its not that hard.
First of all you need to put all of your images in an array
$pictures_array = new Array('picture1.jpg', 'picture2.jpg', '...');
and now, since you know the number of images you have to display you can go with a simple for structure
for($i=0; $i";
unset($pictures_array[$random_key])
}
Hope that helped you :)
o.O
now i look dumb since a part of the code i wrote got ripped of for some kind of reason
That is definitely not the cleanest way, but if it works, it works!
Great article – though why is it ‘dirty’? It does the job!
I think Chris S. is calling it “dirty” just because it’s not very dynamic. If the number of images changes or the file names of the images, you’ll need to go into the code an manually change things. There are some fancy-pants methods out there which can read the files names from a folder and display random images from there, which is much less quick and less dirty.
But I agree, this is a very simple and effective way of doing random images I’m sure I’ll end up using it somewhere soon!
Why not set a php-file for background-image in the CSS? As long as you’ve set a fixed width/height, it’ll render as it should. Then you could just use a class/id on an element instead of using the img-tag at all.
Because you need to have that setup in your configuration. It won’t work on all servers to use a php extension as an image.
Also you can use this to randomize styles in CSS. Something like this:
http://granimpetu.com/articulos/estilos-aleatorios-con-css-y-php/
Ah, very clever Horacio!
Nice and simple, love it. Great job Chris this was really useful for me!
So, my question now is after reading Horatio’s comment, can we use this php for divs containing images and text, or even .css files? Please take a look at jontangerine.com for further evidence that it’s possible. I e-mailed Jon, but he led me down a road to a friend of his, which I’ve yet to hear back from. Any ideas would be great! Thanks