my first php script – dynamically rotating images

I’m proud of myself yesterday afternoon I wrote my first ever tiny php script. I’ve done lots of editing of php, since all the forum software and CMS’s use php, and I’ve become familiar with the syntax. However at university I learn Java and the entire course was entirely object oriented……so I struggled to get my head around a non object oriented language.

Anyway, I’ve given it a break for a while, but yesterday (with the help of Alexander who is on holidays at the moment) I returned to finishing off the new site I’ve been working on for, and Alexander and I were discussing a dynamic image display (like a rotating banner, but not precisely a “banner” for our project). I’m sure it’s easy enough to find thousands of php scripts online that do just the job, but just as I was about to google for one, I said to Alexander “you know what? I should see if I can try and write one myself.” When I said that I didn’t honestly believe I could get a working banner, but Alex had done a similar little task once while trying to learn php and he’s kept telling me I should try and do the same and learn more php this way.

So, I tried it. Started off with a variable, a random number generator and wrote a simple script first that had some details hard coded. With some suggestions from Alex and not much editing, we got it to work! The idea is, that for each random image will display an image with a custom “alt” tag for each image plus a link to a website.

I ended up with this:


/* first integer is minimum and must not be changed. Second integer is number of images – 1 (ie. the size of the array) */
$random_number = rand (0 , 9);
$width = ‘156’; /*width of the images */

/* Array number must match image number image(n).jpg, ie. image0.jpg */
$image[0] = array (‘’, ‘groupfitness website’);
$image[1] = array (‘#’, ‘1’);
$image[2] = array (‘#’, ‘2’);
$image[3] = array (‘#’, ‘3’);
$image[4] = array (‘#’, ‘4’);
$image[5] = array (‘#’, ‘5’);
$image[6] = array (‘#’, ‘6’);
$image[7] = array (‘#’, ‘7’);
$image[8] = array (‘#’, ‘8’);
$image[9] = array (‘#’, ‘9’);

echo ‘<a href=”‘ . $image[$random_number][0] . ‘” target=”_blank”><img border=”0″ width=”‘ . $width . ‘” src=”images/image’ . $random_number . ‘.jpg” alt=”‘ . $image[$random_number][1] . ‘” /></a>’;


Now I’ve set up an array of different images. The one draw back is that everytime we add an image, we have to change the size of the random number generator and add the url and alt tag info, but it’s a simple script that will serve our purpose, and I’m really proud I wrote it myself! 🙂

I have wondered though about the two different methods I’ve used instantiating a multidimensional array. Personally I think that writing:

$image[0] = array (‘’, ‘groupfitness website’);

should be identical to:

$image[0][0] = ‘’;
$image[0][1] = ‘groupfitness website’;

However I’m not sure if the way I have instantiated the variable means that I have created an array, sitting inside an array, which creates the same output but could create extra overhead. Probably not applicable in such a small script as this one, but it’s just got me wondering.

I don’t know the answer, but I’m inclined to think that the way I have done it accesses the array memory space in the same way as the second example, but it’s difficult to find out the answer for sure! If there’s any php guru’s out there I’d love to know the answer to this one!

One thought on “my first php script – dynamically rotating images

  1. Hi Mel,

    My name is Jason and I am a group fitness instructor myself – been teaching Jam and Combat now in Brisbane for about 10 years. Interestingly enough, I’m also a PHP developer and have created a site called My Cardio Club that I am hoping will be the place instructors all around the world can go to find people when they need them, and conversely for instructors to be found for prospective work.

    Anyhow, I’m about to talk to some trademark lawyers (who are asking for a lot of money 🙁 about my ability to use the names [and hopefully logos] of the Les Mills trademarked programs like BODYJAM, BODYCOMBAT, etc. I have actually been a massive fan of your website and your diligent updating it with comprehensive tracklists of all programs for quite some time now – and when I had to start thinking about these potential legal issues, I remembered that you are using the logos and program names on your site.

    I see you’ve got a notice on your site saying that you’ve attained permission from LMI to use their program images and also a disclaimer at the bottom saying that you are in no way affiliated with LMAP. I just have two questions:
    1. How did you get permission to use LMAP images/program names on your site? Do you just contact them and simply ask for it? Or did you have to pay a licensing fee?
    2. What exactly did you need permission to use? Ie do you only need permission to use the logos, or were you fine to use just the program names without the images?

    Anyhow, if you could help me in any way shape or form in relation to these matters, I would be very much appreciative. And apologies for posting this enquiry to such a public forum – I tried to contact you through your personal site, your Facebook page, etc but nothing is allowing me to contact you except your forum lol 🙂

    Hope you’re well. I hope to hear from you soon.

    PS: check out the BODYJAM flash mob we did at FILEX in 2010. I’m the guy in the white hat…. was heaps of fun!

Leave a Reply

Your email address will not be published. Required fields are marked *


To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image