MyBB Source

everyBB - The forum owners community

4,000 Members! | Small Policy Change

Latest Releases: Valid Referrals - Bonus - Mark Thread Read

 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP Random Image/Banner Rotator
Author Message
Mikers
Tutorials Team Member



Posts: 146
Joined: Mar 2010
Reputation: 3
MyBux: 1939
Post: #1
PHP Random Image/Banner Rotator
PHP Random Image Rotator
by Mikers

Hey there gents.

Well, a banner/image rotator is a common want for webmasters out there these days, considering it's uses for advertisement space, or fancy theme design for designers like myself. So for all a' ya, here's a simple way to add in a clean-ish banner/image rotator.

Back-Up The Following Files Before Attempting!
global.php


Alright then, let's get started, shall we?

Start by editing global.php

Find This Code
Code:
$unreadreports = '';
// This user is a moderator, super moderator or administrator
if($mybb->usergroup['cancp'] == 1 || $mybb->user['ismoderator'])
{
    // Read the reported posts cache
    $reported = $cache->read("reportedposts");

    // 0 or more reported posts currently exist
    if($reported['unread'] > 0)
    {
        if($reported['unread'] == 1)
        {
            $lang->unread_reports = $lang->unread_report;
        }
        else
        {
            $lang->unread_reports = $lang->sprintf($lang->unread_reports, $reported['unread']);
        }
        eval("\$unreadreports = \"".$templates->get("global_unreadreports")."\";");
    }
}

Insert Below
Code:
// Image Banner Rotator Script
{
    function getRandomFromArray($ar) {
        mt_srand( (double)microtime() * 1000000 );
        $num = array_rand($ar);
        return $ar[$num];
    }    

    function getImagesFromDir($path) {
        $images = array();
        if ( $img_dir = @opendir($path) ) {
            while ( false !== ($img_file = readdir($img_dir)) ) {
                // checks for gif, jpg, png
                if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
                    $images[] = $img_file;
                }
            }
            closedir($img_dir);
        }
        return $images;
    }

    $root = '';
    // If images not in sub directory of current directory specify root  
    //$root = $_SERVER['DOCUMENT_ROOT'];

    $path = 'images/';

    // Obtain list of images from directory  
        $imgList = getImagesFromDir($root . $path);

    $img = getRandomFromArray($imgList);
}

Change This Line
Code:
    $path = 'images/';
To the path of the images you want to be randomly displayed (such as 'images/rotator/'). I don't recommend leaving the path as 'images/' because the code with randomly display any images you have in that directory.


Save global.php, and exit.

Now go to,
ACP -> Templates & Style -> Templates

Add this code,
Code:
<img src="$path$img" alt="" />
To whatever template that you want your random images/banners to appear at.

Just for those who don't really know how to do that. I'll give you a quick walkthrough to get the random images to display on your index page below the header.

Go to Templates -> Index Page Templates
Edit the "index" template.

Find,
Code:
{$welcome}
{$header}

Add below,
Code:
<img src="$path$img" alt="" />

That code won't center your images, but it's not too difficult with slim knowledge in HTML. But if you're completely devoid of HTML knowledge, here ya go.
<center></center> | Works fine.

Now you're done. To add images to the rotator script, just add images to whatever directory you set. The current code only supports .gif, .jpg, and .png, but you can make it so it'll support others.



And that concludes my small little tutorial.



Credits go to This Site for providing most of the code to customize. I'm a bit lazy when it comes to making my own.

MyBB Source Tutorials
04-26-2010 08:02 AM


telters
Junior Member


Posts: 30
Joined: Jan 2010
Reputation: 0
MyBux: 12
Post: #2
RE: PHP Random Image/Banner Rotator
Kool! Thanks for the great tutorial! I will put that on my site later today!

Check out my website! http://www.grantman.ca It's a blog I Have

[Image: 468_60link_bucks.gif]
06-02-2010 01:06 PM
euantor
Designer/Themer


Posts: 501
Joined: Jun 2009
Reputation: 1
MyBux: 573
Post: #3
RE: PHP Random Image/Banner Rotator
Looks nice Great tut!

Love to play XBox 360? Visit http://www.360elites.net for News, Reviews & Previews of the biggest up-coming and current games like Halo:Reach, Battlefield: Bad Company 2 and more. We have forums too!

Visit http://www.mypurebb.com/ to see some examples of my work with MyBB themes and web designs. I even sometimes take on custom jobs, so why not sign up to the forums so you know when I'm open and designing?
06-02-2010 02:40 PM
 





Web Design | Web Design Jobs