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'] = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIrTH7r-eHeNbK3vEyDyfbBI3Tl6HDAW295tgNVuhrsyYJINiQvZBlikPNl3PgtGlCsEFy9qztaayJ3pB2qwK0VRn0mp9T7grdbRMsotJT7uwCNrYp_7jFS4miD4F3I_eVLL0c0pxEb1A/s1600/security1.PNG";
$banners[0]['href'] = "http://universalcoders.blogspot.in/2015/05/php-validation-filters.html";

$banners[1]['src'] = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirogcCy9C-QEXYFpoMbNVcO5IwEQ2-OPSdjhbuHdSBgtIQ1nJqllMqp8XGIy4LX8Eg4-wKWFSh4_ZllBSF0vsfG71gi6kGh7ZYOwXzAdwCz2jdCe0CNwlXvWp54HC-X4HR2b9iMyBo158/s1600/Screenshot+(21).png";
$banners[1]['href'] = "http://universalcoders.blogspot.in/2015/05/jquery-datatable-plugin-tutorial.html";

$banners[2]['src'] = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjddQ1cXsh9g1OJJ3lowqyg6Hku-g7eWxo4xDfvm5dcZFVChh-eg1tSEkuye0CDnYQi9aOlReaINSG8eZrk2dwhCF9E_VHkdmMch9nNgyA6SteKUktqzOxWCUeNQ0jYYYwVLgRyb_S8e_M/s1600/Screenshot+(15).png";
$banners[2]['href'] = "http://universalcoders.blogspot.in/2015/04/display-thumbnail-image-from-youtube.html";

$banners[3]['src'] = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEit1cGwYaYGMBQI4tSzUA5B2knZahRdEPj-1AkHGhTZmTTuZGGeIJjggHP9nbEP5SyRhb3m3Id_PlzbB5GSqNenFmR-IUHMFUqzw0u33u2Qy2z3gAmWDrv18GIIpfPH8IXApKECk3pSRQY/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>

No comments:

Post a Comment