Forum Moderators: coopster

Message Too Old, No Replies

Ad rotation script

Different results each time called on same page?

         

Justus

5:57 am on Mar 30, 2006 (gmt 0)

10+ Year Member



Hello, I'm using the following script to rotate banner ads on my website

$bannerCounter= 1;

$bannerCode[$bannerCounter] = "<a href=\"http://example.com/categories.php?PARTNER=unstabletable\"><img src=\"http://unstabletable.example.net/affiliate/ad05.jpg\" border=\"0\"></a>";
$bannerCounter++;

$bannerCode[$bannerCounter] = "<a href=\"http://www.example.com/redir.php?aff=8073\"><img src=\"http://unstabletable.example.net/affiliate/hp_ad.gif\" border=\"0\"></a>";
$bannerCounter++;

$bannerAdTotals = $bannerCounter - 1;
if($bannerAdTotals>1)
{
mt_srand((double)microtime() * 1234567);
$bannerPicked = mt_rand(1, $bannerAdTotals);
}
else
{
$bannerPicked = 1;
}
$bannerAd = $bannerCode[$bannerPicked];

And using the following to call it from my pages:

<?
include_once("/home2/thatothe/public_html/unstabletable/ad_rotation.php");
echo "<div align='center'>$bannerAd</div>";
?>

My problem is that I call it multiple times on each page, but it always generates the same ad each time it's called. Make sense? So I end up with the same ad multiple times on each page, when I want *each* one to be generated randomly. Thanks :)

[edited by: coopster at 1:46 pm (utc) on Mar. 30, 2006]
[edit reason] generalized urls [/edit]

tomda

7:13 am on Mar 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why don't you use database.
Just put your banners in a database and do a random query?

Justus

7:21 am on Mar 30, 2006 (gmt 0)

10+ Year Member



Because I don't know mySQL. But if you could give me an example of that, that'd be awesome, and it'd also help me get a start on learning mySQL. Thanks :)

Tastatura

7:39 am on Mar 30, 2006 (gmt 0)

10+ Year Member



“Give man a fish and feed him for a day. Teach him how to fish and feed him for life” :) . There are bunch of free tutorials on the subject, and if you want to buy a book you can find one for $25 or less. Invest a little time, and put valuable tool in your tool box. just my (unsolicited) $0.02

scriptmasterdel

9:30 am on Mar 30, 2006 (gmt 0)

10+ Year Member



First you need to connect to your database with your host, username, password and database name ...


$cn = mysql_connect ("localhost","username","password"); mysql_select_db('database_name');

Then you need to create your banner table so execute this code!
ONLY EXECUTE THIS CODE ONCE


<?
CREATE TABLE `banner`
(
`banner_img` VARCHAR (255) DEFAULT '0'
)

Then once you have the table you simply need to insert the data (img file) ...
Something like ... ONLY EXECUTE THIS CODE WHEN YOU HAVE A NEW BANNER TO ADD ...


<?
$sql = "INSERT INTO banner SET banner_img = 'imgfilehere.gif'";
if(mysql_query($sql))
{
echo 'Banner has been added';
}
else
{
echo 'Banner can\'t be added';
};
?>

Then once you have the banner images stored in the database you need to select a random banner img


<?
$sql_sel = "SELECT * FROM banner ORDER BY banner RAND()";
$res_sel = mysql_query($sql_sel);
$row = mysql_fetch_array($res_sel);

echo '<img src="/folder/'.$row['banner_img'].'">';
?>

NOTE: This is not how i reccomend you should add to the database you should create some kind of management system to manage what banners are visible etc...

... this code hasn't been tested and may not work! sorry ...

However i hope i have helped.

Del

barns101

2:29 pm on Mar 30, 2006 (gmt 0)

10+ Year Member



I currently use phpAdMentor which will allow you to store the banner names in a MySQL database and set ad zones and weight them (i.e. "better" ads will be displayed more often than not-so-good ones). As long as you know a minimum about PHP (from your code above you will be fine) you will be able to set this up and display any number of random ads.

[edited by: coopster at 3:31 pm (utc) on Mar. 30, 2006]
[edit reason] unlinked url [/edit]

scriptmasterdel

3:03 pm on Mar 30, 2006 (gmt 0)

10+ Year Member



Additionally, you could just do a simple array to select a random image and banner ... as follows

<?
// put the image banners in an array
$banners = array("image1.gif","image2.gif","image3.gif");
// put the urls in an array
$urls = array("www.google.co.uk","www.amazon.co","www.yoursite.com");

// count the banners for max randomness ;)
$count = count($banners)-1;

// select the random number
$rand = rand(0,$count);

// select the random url and banner!
echo '<a href="'.$urls[$rand].'"><img border="0" alt="'.$urls[$rand].'" src="'.$banners[$rand].'"></a>';
?>

Is this more what you was looking for?

Del

Justus

7:36 pm on Mar 30, 2006 (gmt 0)

10+ Year Member



Hello, a lot of good tips so far, thanks for the help, everyone (especially del, but I'll also be looking into phpAdMentor). What I think I'll do is try and create my own ad manager with control panel and everything. One more question though...what does purpose does the . in php serve? Example:

<a href="'.$urls[$rand].'">
or
<img src="/folder/'.$row['banner_img'].'">';

Thanks:)

jatar_k

8:13 pm on Mar 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it is the concatenation operator
[php.net...]

also if you want to look into packages take a look at [phpadsnew.com...]

Justus

9:18 pm on Mar 30, 2006 (gmt 0)

10+ Year Member



Found my problem to my original question. Should have been using include instead of include_once