Mod Example – Image Rotator

Mod Example – Image Rotator

Mod description example

  • Author: Mika – http://ni5ni6.com/
  • Title: Image rotator v0.1 created on 18.08.06
  • sNews versions: all
  • Description:
  • – the smallest image rotator in the world
  • – rotate 8 jpg images on every page refresh
  • – images must be named numerically 1-8 (1.jpg, 2.jpg . . . 8.jpg)
  • – images must be uploaded into folder /your-site-root/images/
  • Dependancy: none
  • Screenshots: none
  • Browsers/platforms: all
  • Issues: none
  • Live site example: http://ni5ni6.com/

Mod Usage:
Put this line in your index.php file where you want to display image rotator

<?php rotator();?>

Put this code somewhere in your snews.php file

// MOD – IMAGE ROTATOR
function rotator() {
echo ‘<img src=”images/’.mt_rand(1, 8).’.jpg” alt=”Ni5Ni6.com” />’;
}

Mod improvements

This mod is written too rigidly and, depending on user’s knowledge level, probably not very transparent. There are no comments attached and no room for any further customizaton – the user cannot change image and folder name. Let’s make some adjustments.

Image rotator v1.1

  • Author: Mika – http://ni5ni6.com/
  • Title: Image rotator v1.1 created on 18.08.06
  • sNews versions: all
  • Description:
  • – the smallest image rotator in the world
  • – edit $folder variable according to your preferences:
    1. create a folder on your server
    2. store its name here
    3. upload some images into it
  • – edit the array (provided below in the code) with images stored inside $folder and add some captions as well
  • – image quantity and names are not restricted as in previous v0.1 version
  • – there’s also an optional (commented) code attached, which allows hyperlinking
  • Dependancy: none
  • Screenshots: none
  • Browsers/platforms: all
  • Issues: none
  • Live site example: http://ni5ni6.com/

Mod Usage:
Same as in v0.1. Only function rotator() has changed.

// MOD – IMAGE ROTATOR
function rotator() {
/*
– array solution borrowed from bramsyuur’s Simple ADS Rotation Mod posted at http://www.solucija.com/forum/ on 2006-10-27
*/
/*
extracting variables
– $folder variable allows us to change rotator folder name more easily
– don’t forget the trailing slash
*/
$folder = ‘images/’;
/*
– Array has been introduced to our mod which now has the possibility to contain unlimited number of images as well as the possibility to modify its alt attributes.
– Example:
$img[0][‘src’] = image source
$img[0][‘alt’] = image alt attribute
– Hint: if you need more information from your rotator, expand the array
$img[0][‘href’] = link
– Note: if using expanded version, uncomment corresponding parts of the code
*/
$img[0][‘src’] =’snews.gif’;
$img[0][‘alt’] = ‘snews CMS’;
//$img[0][‘href’] = ‘http://snewscms.com/’;
$img[1][‘src’] = ‘ni5ni6.jpg’;
$img[1][‘alt’] = ‘Coding Workshop Ni5Ni6.com’;
//$img[1][‘href’] = ‘http://ni5ni6.com/’;
$img[2][‘src’] = ‘solucija.png’;
$img[2][‘alt’] = ‘Solucija CSS Templates’;
//$img[2][‘href’] = ‘http://www.solucija.com/’;
/*
array_rand() is a built-in PHP function that returns randomized entry out of an array
*/
$random = array_rand($img);
/*
output result
<img src=”folder/image” alt=”alt attribute” />
*/
echo ‘<img src=”‘.$folder.$img[$random][‘src’].'” alt=”‘.$img[$random][‘alt’].'” />’;
/*
output result with expanded array
<a href=”href value” title=”image alt attribute”><img src=”folder/image” alt=”alt attribute” /></a>
*/
//echo ‘<a href=”‘.$img[$random][‘href’].'” title=”‘.$img[$random][‘alt’].'”>’;
//echo ‘<img src=”‘.$folder.$img[$random][‘src’].'” alt=”‘.$img[$random][‘alt’].'” /></a>’;
}

Conclusion

As you can see, the initial v 0.1 has evolved significantly. The next step could be adjusting the mod to be capable of recognizing the current category and rotating images accordingly, or creating active links (okay, we did that already), or whatever you need from it. Bear in mind that you don’t end up using larger code, when all you really need is a smaller, simpler, and faster solution.

Are you up for a challenge?

GET STARTED

sNews requires PHP, MySQL and mod rewrite. If your server meets those requirements, get started and learn how to install sNews on your website in 3 easy steps.

LEARN

Browse through our help center and learn about template tags and how to simply create your own theme. Dig into sNews and customize it to behave the way you want.

EXTEND

sNews can be a simple blog tool and a full blown CMS. Customize it with addons, mods or play with different themes.

DISCUSS

Talk about sNews on our Forum and share your expirience.