Solved

PHP advertisement rotator

Posted on 2011-03-19
8
317 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:kbit
  • 4
  • 2
  • 2
8 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 450 total points
ID: 35173501
When your iframe fires up, it would look for $_GET["id"] and if it was empty(), the script would SELECT the first advertisement.

Upon selecting the first advertisement, it would produce the meta-refresh tag with the URL containing id=XX, where XX is the id of the next advertisement in the queue.  This process would repeat, with each script producing a new value for id=XX in the refresh tag.

Once the queue is exhausted, the meta-refresh tag would set the URL parameter id=0, which will evaluate the same as empty($_GET["id"]) and the process will repeat.
0
 
LVL 20

Assisted Solution

by:Mark Brady
Mark Brady earned 50 total points
ID: 35173706
And to add to Ray's idea, if you have a database (mysql) setup, I do it this way. Add a column to your adds table (where-ever you keep your banner add information) and call it "active" for eg. Set it to "timestamp" and in your code when you retrieve your current add, put this code in there.

$time = time();

That will return the current timestamp as a number. Insert that number into the banner add that you select to display. Now in the $sql statement to select the next add,

ORDER BY `active` ASC LIMIT 1

to the select code.

What happens is each time you select an add to display, it will add the current timestamp to that add and that will select the oldest add with the lowest number to display. The current add you just selected will get given a highest number so it will go to the bottom of the list and won't get selected again until all the other adds have been displayed. Let me know if you don't understand but it's a simple yet effective idea I use on commercial websites.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35173766
@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!
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 35174671
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?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35175175
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
0
 

Author Comment

by:kbit
ID: 35182466
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.
0
 

Author Comment

by:kbit
ID: 35187570
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

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 35198980
Thanks for the points - it's a really good question! ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now