MyBB Source


MyBB 1.4 Support Now Discontinued

Latest Releases: [For 1.6] Mark Thread Read - [For 1.6] Guest Access - [For 1.6] Awards System v2.0

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



Posts: 147
Joined: Mar 2010
Reputation: 3
MyBux: 1941
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: 534
Joined: Jun 2009
Reputation: 1
MyBux: 605
Post: #3
RE: PHP Random Image/Banner Rotator
Looks nice Great tut!

06-02-2010 02:40 PM
Don+
Junior Member


Posts: 44
Joined: Aug 2010
Reputation: 0
MyBux: 6
Post: #4
RE: PHP Random Image/Banner Rotator
wow, its a nice modification. Thanks
08-21-2010 10:17 AM
1master1
Junior Member


Posts: 20
Joined: Aug 2010
Reputation: 0
MyBux: 0
Post: #5
RE: PHP Random Image/Banner Rotator
thanks for this great tutorial
10-13-2010 09:44 PM
zsulboy
Junior Member


Posts: 8
Joined: Oct 2010
Reputation: 0
MyBux: 6
Post: #6
RE: PHP Random Image/Banner Rotator
Thanks! For this great tutorial!
Yesterday 06:50 PM
 





Artist Community | GFX forums