Recently, a friend and reader of mine, Trina, asked me on Facebook on how did I manage to get the images you see in the current design’s header to load randomly upon page load. The images in the header are loaded into a glider container, which I used Glider.js to allow for smooth transitioning among the images.
In this tutorial, I will not be covering the details of constructing a glider groundup because that has been covered in a previous tutorial, Compacting contents using Glider script. I will be covering more on how we will be loading in images using php.
There are many other gallery/glider scripts that use different javascript libraries out there – Glider.js relies on scriptaculous and prototype, which I know have fallen out of favour as jQuery becomes more widely adopted. However, the basic construction remains the same and the steps below will still remain relevant to those using
Setting up the Glider
The basic construct of a typical glider will be as follow, according to the Glider tutorial:
<div id="glider"><div class="scroller"><div class="content"><div id="section1" class="section">...</div><div id="section2" class="section">...</div><div id="section3" class="section">...</div></div></div></div><script type="text/javascript" charset="utf-8">var my_glider = new Glider('glider', {duration:0.5});</script>
Instead of placing texts within each individual sections in the previous tutorial, we will be inserting images into the <div> elements instead:
<div id="glider"><div class="scroller"><div class="content"><div id="section1" class="section"><img src="..." alt="..." title="..." /></div><div id="section2" class="section"><img src="..." alt="..." title="..." /></div><div id="section3" class="section"><img src="..." alt="..." title="..." /></div></div></div></div><script type="text/javascript" charset="utf-8">var my_glider = new Glider('glider', {duration:0.5});</script>
But then of course, we will be inserted images dynamically into the glider, and not by copy and pasting every single line. We will be using the for loop for this – keep this in view, we’ll come back to the for loop later.
Rotating the images
My strategy was very rudimentary and perhaps imperfect, so if you have any improvements, feel free to voice up in the comments section below. The strategy is simple – to ensure a wide variety of images, I cluster my images into different folders based on the theme. Let’s say we want to rotate photos of different types – architecture, food, street and people. I would love to display a random image from each type. What I do is to place the images in different folders located in a main folder called /gallery/ – let’s say 10 photos in a folder named /ach/ (so it’s location will be /gallery/ach/), and so on and so forth for the remaining categories.
Now we will need to make a request to the server as the page loads – asking it to serve us a random image from each category. This is achieved by using the lightweight 4kb image rotation script (view PHP source) written by Dan Benjamin for A List Apart. Place the file rotate.php into each folder, and when we load the PHP file as an image source, we will be served a random image by the php file, for example:
<img src="/ach/rotate.php" alt="Photo - Architecture" title="Photo - Architecture" />
However, we will also need to jumble up the sequence of which the category are loaded. Therefore, randomity of the images come in 2 forms – an image is randomly loaded from a category, and the order of the images are further randomized.
Jumbling up the order of images
What we need to do to jumble up the order of the image will be as follow:
- Create an array with a strings
- Shuffle the array so that each string will be called in random order
- Execute a for loop so that we run the loop as many times as the size of the array
- In the for loop, load rotate.php from each category folder
And the ideas above can be translated into php as seen below:
<?php$gliderdata = array('ach|Architecture','fod|Food','str|Street''ppl|People',);shuffle($gliderdata);for ($i = 0; $i < count($gliderdata); $i++){$gliderimg = explode('|', $gliderdata[$i]);echo '<div class="section" id="section'.($i+1).'"><img src="'.get_bloginfo(stylesheet_directory).'/gallery/'.$gliderimg[0].'/rotate.php" alt="Photo - '.$gliderimg[1].'" title="Photo - '.$gliderimg[1].'" /></div>';}?>
In the for loop, what I did was to explode the string in $gliderdata with ‘|’ as the delimiter – for example, if we explode ach|Architecture, we will separate them into ach and Architecture – they will be referenced as $gliderimg[0] and $gliderimg[1] respectively.
After that, we will echo the individual sections of the glider which will house the image elements. The sections will be assigned a unique identifier, section'.($i+1).', will mean that for each iteration of the for loop, the identifier will reflect the number of iterations. The reason why $i+1 is used is because $i starts from zero but the section identifier has to start from “section1″ and more.
I have placed the /gallery/ folder into my WordPress theme folder, but of course you can use a static URL in its place. Instead of using '.get_bloginfo(stylesheet_directory).'/gallery/'.$gliderimg[0].'/rotate.php, we can use http://yoursite.com/gallery'.$gliderimg[0].'/rotate.php. Remember that $gliderimg[0] refers to the name of the folder while $gliderimg[1] refers to the description.
In short, the elements in the array should be formatted with this in mind: '[foldername]|[description]',. Whenever you add a new directory to the gallery folder, remember to add a new line to the array with the new subfolder name and a description if you feel like it. Each subfolder should contain photos and the rotate.php file.
Final code
We compile everything together into a decent glider with does not only jumble the sequence of images, but also loads a random image from a folder.
<div id="glider"><div class="scroller"><?php$gliderdata = array('ach|Architecture','fod|Food','str|Street''ppl|People',);shuffle($gliderdata);for ($i = 0; $i < count($gliderdata); $i++){$gliderimg = explode('|', $gliderdata[$i]);echo '<div class="section" id="section'.($i+1).'"><img src="'.get_bloginfo(stylesheet_directory).'/gallery/'.$gliderimg[0].'/rotate.php" alt="Photo - '.$gliderimg[1].'" title="Photo - '.$gliderimg[1].'" width="270" height="180" /></div>';}?></div></div><script type="text/javascript" charset="utf-8">var my_glider = new Glider('glider', {duration:0.5});</script>
If you have any questions, feel free to contact me, or ask in the comments section.



















Woohoo! *HUGS* You are brilliantly awesome. Thanks, Teddy. And you wrote it up so quick too. I really appreciate it… lots and lots. Now I can’t wait to go and try it out!
Btw, I love your tutorials. You make something that would normally sound a little overwhelming… simple. Always very easy to understand. Thanks!!
I’m glad that this tutorial helped, Trina. I have a feeling that this tutorial could have been written better but I couldn’t find a way to better describe it and pen the annotations. For the moment being, please bear with it, heh.
Oh, I’m sure you did just fine. :) I have not been able to mess around with it yet due to that horrid bug that I had. BUT, I’m hoping to have time to work on it tonight — I’m so eager to get this blog done. I do miss writing!
Thanks again! <3
Thank you Trina! I wasn’t quite in the right mind when I was writing the tutorial – I was chatting with another friend and was using a split screen. Half of it on Skype, the other half on WordPress dashboard, haha.
What was the horrible bug that you’re having? I might be able to help ;) just shoot me an email or something.
I can’t wait to see your new layout too! I know you’ll churn out some quality work. Take your time, heh.
Hehe, the bug I was referring to is the stomach flu that I had. Which kept me in bed most of the week so I was unable to work on the coding. :P
I have a question, Teddy. The line you have included up there ^ starting with:
<img src="/ach/rotate.php" alt="Photo - Architecture" title="Photo - Architecture" />Is that actually included somewhere in the code? If so, where?
Yeap it is. In fact, the line is dynamically generated in the PHP script that I have included. Remember the arrays at the start of the script? One of the line went
'ach|Architecture',. The|is the delimiter (which we will use later to separate the two words).achwill be the name of the folder whileArchitectureis simply the description.If you look further down, the for loop contains the code:
echo '<div class="section" id="section'.($i+1).'"><img src="'.get_bloginfo(stylesheet_directory).'/gallery/'.$gliderimg[0].'/rotate.php" alt="Photo - '.$gliderimg[1].'" title="Photo - '.$gliderimg[1].'" /></div>';…which will actually print out the following:
The [foldername] and [description] has been specified in the array as
'[foldername]|[description]',. In this case we have'ach|Architecture',.The other lines in the array will also be printed, but the overall order will be shuffled every time the page loads because of the
shuffle()function.Oh okay. Thanks for clearing that up for me. :)
One more question — I finally have it set up decently (I’m still not sure if everything is quite right yet lol) On each page load, a random photo does load, but I’m unable to stream through the rest of that gallery. For instance, I click the “Next” or “Previous” button and no other photo pops up. Does that make sense? lol :|
There are two probable reasons that can explain your problem:
1. Did you include more than one element in the array? This is because if you have a single photo only, the glider will not scroll (I think).
2. The glider installation is faulty. I would suggest that you double and triple check the script on the page – that the glider.js, prototype, scriptaculous libraries have been properly loaded. Also, remember that you will need to specify the glider container in the javascript after the glider container, as seen below: