Free Banner Rotator Code

Many of my friends from faucet owner lobby are asking for a simple and effective code for banner rotator which they can use anywhere they want. So i came to conclusion with these codes. And i found them simple and effective.

Click To Check Simple Banner Rotator Code Without MYSQL

Learn How To Make Free PHP MYSQL Banner Rotator
STEP 1 :-

First make an sql table in your existing database, or if u are not using any database then first create database and then run this query in SQL tab of phpmyadmin.

CREATE TABLE `banners` (
`id` int(50) NOT NULL AUTO_INCREMENT,
`code` text NOT NULL,
`status` int(5) NOT NULL,
`size` varchar(150) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
STEP 2 :-

Ad this form code where ever u want backend to be available. This form will add banners and codes to your database. Add css styling by yourself as this change with every template. I am providing only form code.

<!-- FORM TO ADD BANNER -->
<form action="" method="post">
  <input type="text" name="adsize" placeholder="Size of the Banner" required /><br />
  <textarea name="adcode" placeholder="Advertisement Code" required></textarea><br />
  <input type="radio" name="status" value="1"/>Enable | <input type="radio" name="status" value="2"/>Disable <br />
  <input type="submit" value="Submit" name="bannerform" />
</form>
<!-- FORM TO ADD BANNER -->

 

STEP 3 :-

Add this line in same page where banner HTML form is available.  This code will connect Form with Database and register your advertisement code into it.

<?php
#ADD BANNER TO DATABASE#
if(isset($_POST['bannerform'])){
  $advertsize = trim($_POST['adsize']);
  $advertcode = trim($_POST['adcode']);
  $status = trim($_POST['status']);
  if(empty($_POST['adsize']) OR empty($_POST['adcode']))
  {
    echo 'Sorry required field left empty';
  }
  else
  {
    $advertcode = base64_encode($advertcode);
    $sql = "INSERT into `banners` (code,status,size) VALUES ('$advertcode','$status','$advertsize')";
    $result = $dbcon -> query($sql);
    if(!empty($result))
    {
      echo 'Advertisement successfully registerd';
    }
    else
    {
      echo 'Failed to register';
    }
  }
}
#ADD BANNER TO DATABASE#
?>

 

STEP 4 :-

Add this function in you existing function.php file or if there is none. Then make a function.php file and add this code make sure to use it with <?php  and ?> tags because this is php function. And add that page to the page where you want to display Banner rotator.

#FUNCTION FOR BANNER ROTATOR#
function bannerRot($size){
  global $dbcon;
  $sql = "SELECT `code` FROM `banners` WHERE `size` = '$size' AND `status` = 1 ORDER BY RAND() LIMIT 1";
  $result = $dbcon -> query($sql);
  if(mysqli_num_rows($result) > 0){
    $row = $result -> fetch_assoc();
    return base64_decode($row['code']);
  }
}
#FUNCTION FOR BANNER ROTATOR#

 

STEP 5 :-

This step is for those who have no existing Database connection page. Make a connection page and write this code into it and change details.

#CONNECTION WITH MYSQL#
$dbcon = new mysqli('localhost','root','databasepassword','test_rotator');
#CONNECTION WITH MYSQL#
localhost -> almost every server is having this value as it is or may be 127.0.0.1
root -> will change to your database username
databasepassword -> will change with your database password
test_rotator -> will change with database name
STEP 6 :-

Use  <?php echo bannerRot(‘468×60’); ?>  wherever you want to display 468×60 size banners.

For example if ur size is 300×250 , so while registering banner give size name 300×250 and then use like this
<?php echo bannerRot(‘300×250’); ?> where u want to display 300×250 size banner.