Link to home
Start Free TrialLog in
Avatar of kbit
kbit

asked on

PHP advertisement rotator

Hi I have a webpage that contains an iframe.

This iframe reads a mySQL database and selects advertisements from a table, in sequence.

I need this iframe to automatically refresh after 20 seconds and every time it does, it needs to show the next advertisement, not a random one.

Can someone please show me how to set up the loop, below are my efforts so far?

Thanks in advance
$query="SELECT * FROM advertisements WHERE AdvertisementStatus='Active' ORDER BY AdvertisementID";
$result = mysql_query($query) or die("Couldn’t execute query.");
$num=mysql_num_rows($result);

while ($row = mysql_fetch_array($result))
{
$AdvertisementLink=$row['AdvertisementLink'];
?>
<img src="<?php echo $AdvertisementLink; ?>" border="0" alt="Top advertisement">
<meta http-equiv="refresh" content="20" />
<?php
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@elvin66:  I thought about this and wondered if script racing might interrupt the orderly flow of advertisements.  It would probably depend on how you define "orderly."  If you have several clients accessing the site simultaneously you might not be able to control the order of the advertisements on a client-by-client basis.  That may or may not matter to our Asker.  It would seem to me that we would need to make a choice whether to show the ads to each individual in a given order, or to show the ads to our entire audience in a given order.  If it were up to me, I would show the ads in descending order sorted by price paid!
I agree Ray, order by price paid as that is the fairest system of all. The more you pay, the more impressions your add gets. On one of my commercial sites I rotate the adds and as you say, they are rotated independent of each user but each impression adds the time() to the table and that affects it for everyone. If you had a million people on the site per week it would act quite erratically and one particular user could actually end up seeing the same add each time he/she refreshed the page as other users are changing the timestamp. You could of course set it up where each user sees the ads in proper rotation but it depends on what the asker really wants. Nice to see you back on the questions Ray - haven't seen you for a while - been bust making truck loads of money huh?
Actually, I've been busy with the start of baseball season!  But beginning Monday I will be starting a new gig with personal video messaging.  I expect to still have some time for EE throughout the workday.  For me, it's a good way to keep sharp.

Cheers, ~Ray
Avatar of kbit
kbit

ASKER

Thanks for the suggestions both, I'll try these out tomorrow. The end-user will sequence the advertisements based on the money received. This decision and the payments themselves will be offline. Then I'll be sorting the ads by their Sequence Number, this determines who appears first, second etc.
Avatar of kbit

ASKER

Thanks again for your help, I got this to work as shown in the iframe code below.
<?php
session_start();
// connect to database
include('connect.php');
$dbLinkID = mysql_connect($server,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$thepage=$_GET['page'];
$currentadvertisementid=$_GET['advertisement'];

//find the smallest active advertisement number for the top of the page where the advertisement number > the previous one
//then show this for 20 seconds

if ($currentadvertisementid=='')	//first loop
{
	$currentadvertisementid='0';
}

$query="SELECT MIN(SequenceID),AdvertisementID,AdvertisementLink FROM advertisements WHERE Page='home' AND Location='top' AND AdvertisementStatus='Active' AND AdvertisementID > '$currentadvertisementid' ORDER BY SequenceID";

$result = mysql_query($query) or die("Couldn’t execute query.");
$num=mysql_num_rows($result);
$details = mysql_fetch_array($result);
$currentadvertisementid = $details['AdvertisementID'];
$AdvertisementLink = $details['AdvertisementLink'];

if ($currentadvertisementid=='') //no more advertisements, start again
{
	$currentadvertisementid='0';

$query="SELECT MIN(SequenceID),AdvertisementID,AdvertisementLink FROM advertisements WHERE Page='home' AND Location='top' AND AdvertisementStatus='Active' AND AdvertisementID > '$currentadvertisementid' ORDER BY SequenceID";

$result = mysql_query($query) or die("Couldn’t execute query.");
$num=mysql_num_rows($result);
$details = mysql_fetch_array($result);
$currentadvertisementid = $details['AdvertisementID'];
$AdvertisementLink = $details['AdvertisementLink'];
}
?>
<img src="<?php echo $AdvertisementLink; ?>" border="0" alt="Top advertisement">
<meta http-equiv="refresh" content="5;url=ad_top.php?page=top&advertisement=<?php echo $currentadvertisementid; ?>" />

Open in new window

Thanks for the points - it's a really good question! ~Ray