Author Topic: [Tutorial]  Simple Banner Ad Rotator in PHP  (Read 10986 times)


0 Members and 1 Guest are viewing this topic.

June 29, 2013, 01:20:43 AM

Offline Brian

  • Administrator
  • Jr. Member
  • *
  • Posts: 49
  • Country: us
  • Karma: 0
    • View Profile
[Tutorial] Simple Banner Ad Rotator in PHP
« on: June 29, 2013, 01:20:43 AM »

(Register to disable this ad!)
"ns"


Here's a simple random ad rotator, written in PHP that's simple and easy to use:

With the comments:
Code: [Select]
<?php
// Start an array

     $ads = array();

// Now, let's create our first ad. We simply just put the
// format of the ad within the $ads[] variable.
// You can create as many ad's as you wish, as we are using
// an array and it won't overwrite the variable.

     $ads[] = '<a href="http://www.smfhelp.com"><img src="http://www.smfhelp.com/Tutorials/Ad1.gif" alt="Ad ALT info" /></a>';
     $ads[] = '<a href="http://www.smfhelp.com"><img src="http://www.smfhelp.com/Tutorials/Ad2.gif" alt="Ad ALT info" /></a>';
     $ads[] = '<a href="http://www.smfhelp.com"><img src="http://www.smfhelp.com/Tutorials/Ad3.gif" alt="Ad ALT info" /></a>';
     $ads[] = '<a href="http://www.smfhelp.com"><img src="http://www.smfhelp.com/Tutorials/Ad4.gif" alt="Ad ALT info" /></a>';

// There, we have ads ready to go.
// You can always add more at any time using the variable below,
// just ad your code in between the ''
// $ads[] = '';
// Use the PHP shuffle function to randomize the advertisements.

     shuffle($ads);

// Now let's display the result from
// the shuffle using the php echo function.

     echo '<center>' . $ads[0] . '</center>';
?>

You can have as many ads as you want and it also works with SMF ad mods and PHP portal blocks, or by manually inserting it into your PHP templates.

Here's the code without the comments:

Code: [Select]
<?php
     $ads = array();

     $ads[] = '<a href="http://www.smfhelp.com"><img src="http://www.smfhelp.com/Tutorials/Ad1.gif" alt="Ad ALT info" /></a>';
     $ads[] = '<a href="http://www.smfhelp.com"><img src="http://www.smfhelp.com/Tutorials/Ad2.gif" alt="Ad ALT info" /></a>';
     $ads[] = '<a href="http://www.smfhelp.com"><img src="http://www.smfhelp.com/Tutorials/Ad3.gif" alt="Ad ALT info" /></a>';
     $ads[] = '<a href="http://www.smfhelp.com"><img src="http://www.smfhelp.com/Tutorials/Ad4.gif" alt="Ad ALT info" /></a>';

     shuffle($ads);
     echo '<center>' . $ads[0] . '</center>';
?>

If you don't want the ads centered, just replace the last line with:

Code: [Select]
     echo $ads[0];

This is the same basic code SMF Help is using to show random banners in the header.  8)
« Last Edit: June 27, 2014, 11:56:21 AM by Brian »

October 12, 2015, 02:47:39 AMReply #1

Offline nend

  • Jr. Member
  • *
  • Posts: 2
  • Karma: 1
    • View Profile
    • SI Community
Re: Simple Banner Ad Rotator in PHP
« Reply #1 on: October 12, 2015, 02:47:39 AM »
Wouldn't it be simpler just going.


$ads
=array(
      
'<html here>',
     
'<more html>',
     
'<more html>,
);


Both correct though. :-D

I would of put up a better example, but being on a 4inch mobile device it takes allot of work,lol.

Tags:
php 
 

Namecheap