Jump to content

Random Ads


Sephaeriud

Recommended Posts

Hi all,

I'm using a Random Ad script on a site I'm working on found here.

 

It's working great, super easy to set up, however the problem is that sometimes it "randomizes" the same ad more than once per page. The site I'm building will have 4 spots for ads per page.

 

Is there a way to script "if -ad- is already displayed, choose a different one"? To avoid having duplicates each time the page is refreshed.

 

Thanks!

Link to comment
Share on other sites

The code for randomad.php is kinda simple, I decided to make a better version while keeping the ad file the same.

 

<?php
/*
Please note for using more than one add per page your need to give each ad a unqiue ID (see the ?ID=x)
You can also add a lifespan but adding &life=x this mean that THIS advert space will not that the same advert got X turns (see third example)
*/

/* SAMPLE
<iframe marginwidth="0" marginheight="0" width="468" height="60" scrolling="no" frameborder=0 src="randomad.php?ID=Top"></iframe>
<iframe marginwidth="0" marginheight="0" width="468" height="60" scrolling="no" frameborder=0 src="randomad.php?ID=Left"></iframe>
<iframe marginwidth="0" marginheight="0" width="468" height="60" scrolling="no" frameborder=0 src="randomad.php?ID=Right&life=10"></iframe>
*/
$adfile = "ads.txt"; //Add file

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
session_start();
// load the file that contain the ads
$ID = (int)(!empty($_GET['ID']))?$_GET['ID']:1;
$ads = file(dirname(__FILE__)."/$adfile");
if(isset($_SESSION["Ad"]) && is_array($_SESSION["Ad"]))
{
foreach(array_keys($_SESSION["Ad"]) as $saveAd)
{
	unset($ads[$_SESSION["Ad"][$saveAd]['Display']]);
	if($_SESSION["Ad"][$saveAd]['Life']==0)
	{
		unset($_SESSION["Ad"][$saveAd]);
	}
	if($saveAd == $ID) $_SESSION["Ad"][$saveAd]['Life']--;
}
}

array_unique($ads);//Remove identcial Ads

$_SESSION["Ad"][$ID]['Display'] = array_rand($ads);
$_SESSION["Ad"][$ID]['Life']=(int)(!empty($_GET['life']))?$_GET['life']:count($ads);
echo $ads[$_SESSION["Ad"][$ID]['Display']];
?>

Link to comment
Share on other sites

Thank you so much, MadTechie, for taking the time to help, however I'm afraid this still isn't working.

 

I gave each ad a unique ID, but I am still getting duplicates. Am I supposed to edit some of the PHP myself to specify each ID?

 

Please help me understand what I'm doing wrong :)

 

Thank you again,

Link to comment
Share on other sites

Just reviewed and found bug, and made a few tweaks..

 

<?php
/*
Please note for using more than one add per page your need to give each ad a unqiue ID (see the ?ID=x)
You can also add a lifespan but adding &life=x this mean that THIS advert space will not that the same advert got X turns (see third example)
*/

/* SAMPLE
<iframe marginwidth="0" marginheight="0" width="468" height="60" scrolling="no" frameborder=0 src="randomad.php?ID=Top"></iframe>
<iframe marginwidth="0" marginheight="0" width="468" height="60" scrolling="no" frameborder=0 src="randomad.php?ID=Left"></iframe>
<iframe marginwidth="0" marginheight="0" width="468" height="60" scrolling="no" frameborder=0 src="randomad.php?ID=Right&life=10"></iframe>
*/
$adfile = "ads.txt"; //Add file

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
session_start();
// load the file that contain the ads
$ID = (int)(!empty($_GET['ID']))?$_GET['ID']:1;
$ads = file(dirname(__FILE__)."/$adfile");

array_unique($ads);//Remove identcial Ads
if(isset($_SESSION["Ad"]) && is_array($_SESSION["Ad"]))
{
foreach(array_keys($_SESSION["Ad"]) as $saveAd)
{
	if($saveAd == $ID) $_SESSION["Ad"][$saveAd]['Life']--;
	if($_SESSION["Ad"][$saveAd]['Life']==0)
	{
		unset($_SESSION["Ad"][$saveAd]);
		continue;
	}
	unset($ads[$_SESSION["Ad"][$saveAd]['Display']]);
}
}
$c = array_rand($ads);
if(is_int($c)) $_SESSION["Ad"][$ID]['Display'] = $c;
$_SESSION["Ad"][$ID]['Life']=(int)(!empty($_GET['life']))?$_GET['life']:1;
echo $ads[$_SESSION["Ad"][$ID]['Display']];
?>

 

depending on the number of ad's you have and the amount per page they may not shuffle very well, (ie with 4 ads and 3 per page they only move 1 step, but it should be fine,

 

If you want to allow them to reshuffle then you could add this on the page (not in the randomad.php file) will reset them

session_start();
$_SESSION["Ad"] = array();

 

*what i mean by reshuffle is if you have 4 ads and 4 per page no ads will move from box to box, but the code above will resolve that

Link to comment
Share on other sites

First, thank you again for your help,

 

I currently have 5 ads, and 4 spots per page.

 

Ideally, what I'm looking for is when the page is refreshed, you will get one of the 5 ads randomly in each spot, but without duplication - as in displaying the same ad more than once per page.

 

This new code made the 4 ads static, they don't change when the page is refreshed.

 

Where do I need to place the reshuffle code to allow them to randomize when the page is refreshed?

 

Thank you,

Link to comment
Share on other sites

If you add more ad your see they do shuffle around,

 

you could add the re-shuffle to the page display itself,

 

for example (please note this must be a PHP file)

<?php
session_start();
$_SESSION["Ad"] = array();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>Random Ad Sample</title>
</head>

<script language="JavaScript">
<!--

function color_selected(c)
{
document.getElementById("color_value").value = c;
}

// -->
</script>

<body>

<p align="center"><strong><font color="#336699" size="+1">Fla<font color="#FFCC00">s</font>h-here.com 
  Random Ad Example</font></strong></p>
<p> </p>
<iframe marginwidth="0" marginheight="0" width="468" height="60" scrolling="no" frameborder=0 src="randomad.php?ID=Top"></iframe>
<iframe marginwidth="0" marginheight="0" width="468" height="60" scrolling="no" frameborder=0 src="randomad.php?ID=Left"></iframe>
<iframe marginwidth="0" marginheight="0" width="468" height="60" scrolling="no" frameborder=0 src="randomad.php?ID=Right"></iframe>
<iframe marginwidth="0" marginheight="0" width="468" height="60" scrolling="no" frameborder=0 src="randomad.php?ID=Bottom"></iframe>

<table width="100%" border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td><div align="center"><font color="#666666">© 2003 Flash-here.com (contact: 
        <a href="http://www.flash-here.com">web</a>, <a href="mailto:support@flash-here.com">email</a>)</font></div></td>
  </tr>
</table>
</body>
</html>

Link to comment
Share on other sites

Yes, or increase the number of ad's,

 

let me explain what's happing

you have 5 ads, and 4 on a page,

the page loads

Ad 1 loads up and is added to a list to not display again

Ad 2 loads up and is added to a list to not display again

Ad 3 loads up and is added to a list to not display again

Ad 4 loads up and is added to a list to not display again

 

same page reload,

Ad 1 gets removed from the list BUT Theirs only 2 ads allowed (as the other 3 are still locked)

theirs a 50% chance of the same ad being used, (of course if you only had 4ad's theirs 100% chance)

 

What that code does is release all ad's when the page loads,

 

Because your using iFrames it makes the whole task a little harder as each page is independent

Link to comment
Share on other sites

if you don't mind inline PHP then i could create a class,

then your just enter

<?php echo showAd(); ?>

in the places you want an ad to appear

your also need

<?php include "AdClass.php"; ?>

or something at the start of the page

 

if thats okay i'll write one for you

Link to comment
Share on other sites

Okay i wrote a basic one and then added a few useful things

 

instructions below

 

Save this to a file called "Advert.class.php"

<?php
/**
* SAC (Simple Adverts Class)
* Usage: 
*/
if (! defined("PHP_EOL"))
{
define("PHP_EOL", strtoupper(substr(PHP_OS, 0, 3) == "WIN") ? "\r\n" : "\n");
}

class Adverts
{
protected $Ads = array();
protected $adFile = "";
    
public function __construct($adfile)
{
	if(!file_exists($adfile)) trigger_error("Ad file not found!",E_USER_WARNING);
	$this->adFile = $adfile;
	$this->Ads = file($this->adFile);
}

/**
* RemoveDups
* This removes any duplicate Adverts
*/
public function RemoveDups()
{
	array_unique($this->Ads);//Remove identcial Ads
}
public function removeAd($c)
{
	unset($this->Ads[$c]);
}

/**
* getAd Returns a Random Advert, is clear is set to true 
* the returned advert will be removed from the list
*/
public function getAd($clear=true)
{
	$c = array_rand($this->Ads);
	$Ad = $this->Ads[$c];
	if($clear) $this->removeAd($c);
	return $Ad;
}

}

class AdvertControl extends Adverts 
{
function __construct($adfile)
{
	parent::__construct($adfile);
}
function addAd($NewAd="")
{
	if(empty($NewAd)) trigger_error("Faild to Add Ad, (nothing to add)",E_USER_ERROR);

	$handle = fopen($this->adFile,"a");
	fwrite($handle,$this->filter($NewAd).PHP_EOL);
	fclose($handle);
}

function updateAd($ID, $value)
{
	$this->Ads[$ID] = $this->filter($value);
}

function filter($NewAd)
{
	return str_replace(PHP_EOL,"",$NewAd);
}

function rebuildFile()
{
	$handle = fopen($this->adFile,'w+');
	foreach($this->Ads as $Ad)
	{
		fwrite($handle,$this->filter($Ad).PHP_EOL);
	}
	fclose($handle);
}
function adControl()
{
	echo "<h1>Basic Controls</h1><br />";
	if(isset($_GET['DeDup']))
	{
		$this->RemoveDups();
		$this->rebuildFile();
	}
	if(isset($_GET['update']))
	{
		$this->updateAd($_GET['update'], nl2br($_POST['Advert']));
		$this->rebuildFile();
	}
	if(isset($_GET['remove']))
	{
		$this->removeAd($_GET['remove']);
		$this->rebuildFile();
	}
	foreach($this->Ads as $K => $V)
	{
		echo "<a href=\"?remove=$K\">Remove<a>\t";
		echo "<a href=\"?edit=$K\">Edit<a>\t";
		if(isset($_GET['edit']) && $_GET['edit']==$K)
		{
			$this->EditAd($K,$V);
		}else{
			echo "$V<br />\n<br />\n";
		}
	}
	echo "Add Advert<br />\n";
	$newAdID = count($this->Ads);
	$this->EditAd($newAdID,"New Advert");
	echo "<br />";
	echo "<a href=\"?DeDup=true\">Remove Duplicates from master file</a>";
}
function EditAd($ID,$Ad)
{
	echo "<form action=\"?update=$ID\" method=\"post\">";
	echo "<textarea style=\"width: 600px; height: 150px;\" name=\"Advert\">";
	echo htmlspecialchars($Ad,ENT_QUOTES);
	echo "</textarea>";
	echo "<br />\n";
	echo "<input type=\"submit\" name=\"Save\" value=\"Save\">";
	echo "</form>";	
}
}
?>

 

 

And this is a demo page

<?php
$adfile = "ads.txt"; //Add file
require_once("Advert.class.php");
$Ad = New Adverts($adfile);

/*when i save removes the ad i mean it won't appear on the page again!*/
echo $Ad->getAd(false); //Shows an Add BUT doesn't remove it (you probably won't use this)
echo $Ad->getAd();//Shows an Add and removes
echo $Ad->getAd();//Shows an Add and removes
echo $Ad->getAd();//Shows an Add and removes
echo $Ad->getAd();//Shows an Add and removes
echo $Ad->getAd();//Shows an Add and removes
echo $Ad->getAd();//Shows an Add and removes
?>

 

 

 

Another page (basic admin)

<?php
/*
some very basic controls (like add/edit/remove ad's)
*/
require_once("Advert.class.php");
$adfile = "ads.txt"; //Add file
$Ad = New AdvertControl($adfile);

//Add an advert
//$Ad->addAd("Testing"); add a advert

//Basic Admin control
$Ad->adControl();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.