Saturday, 13 June 2015

Banner Ad Rotator Using PHP, Jquery & Ajax




Banner Ads are basically used in Affiliate marketing which may differ based on the services i.e. CPM (Cost per Impression), CPC (Cost per Click) or CPA (Cost per Action) basis which may contain following functionalities as follows :

CPM : Count number of impressions.
CPC : Count number of clicks.
CPA : Count number of successful sales.

Today we will learn how to display random banner rotator using PHP, Jquery & Ajax without any count :



--------------------------------------------------------------------------------------------------------------------

 File : index.php

<html>
<head>
<title>Banner Ad Rotator</title>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$("document").ready(function(){
    setInterval(function(){
        $.ajax({
            type:"POST",
            url:"ajax.php",
            success:function(result){
                $("#banner_div").html(result);
            }
        });
    }, 2000);
});
</script>
</head>
<body>
<div id="banner_div"></div>
</body>
</html>

-----------------------------------------------------------------------------------------------------------------

File : ajax.php

<?php
$banners = array();
$banners[0]['src'] = "http://1.bp.blogspot.com/-dWd_4cecNMY/VViUoLTTItI/AAAAAAAAA-0/1CZq7igFz6Y/s1600/security1.PNG";
$banners[0]['href'] = "http://universalcoders.blogspot.in/2015/05/php-validation-filters.html";

$banners[1]['src'] = "http://2.bp.blogspot.com/-Aj_ftf_TvWo/VUSQGD0KOcI/AAAAAAAAA-Q/91VaIa24-yI/s1600/Screenshot%2B%2821%29.png";
$banners[1]['href'] = "http://universalcoders.blogspot.in/2015/05/jquery-datatable-plugin-tutorial.html";

$banners[2]['src'] = "http://4.bp.blogspot.com/-Q6bCVs-u1wQ/VTM8nriqJzI/AAAAAAAAA8w/vB2QSZnYam8/s1600/Screenshot%2B%2815%29.png";
$banners[2]['href'] = "http://universalcoders.blogspot.in/2015/04/display-thumbnail-image-from-youtube.html";

$banners[3]['src'] = "http://1.bp.blogspot.com/-X59iN00Ant4/VTIbcjbqT-I/AAAAAAAAA8E/9U8nRWp4kB0/s1600/Screenshot-11.png";
$banners[3]['href'] = "http://universalcoders.blogspot.in/2015/04/call-function-catquery-select.html";

$min = 0;
$max = count($banners) - 1;

$current_ad = mt_rand($min,$max);
?>
<a href="<?php echo $banners[$current_ad]['href'] ?>" title="">
    <img width="400" height="300" src="<?php echo $banners[$current_ad]['src'] ?>" alt="">
</a>