Page 1 of 1

better ad rotator

Posted: Sun Jun 24, 2012 6:07 pm
by Vegan
a while ago I cobbled together a simple ad rotator

now I am looking towards a more sophisticated version

display more than 1 ad and no show any dupes

Code: Select all


<?

// Tower Ads intended for display below the navigation block
// The ad size needs to be 120x600 which is called a wide scyscraper

$ads = array(
	array(
		'insert ad 1 here'
	),
	array(
		'insert ad 2 here'
		),
	),
);
 
$ad = $ads[array_rand($ads)];
echo $ad;

?>


Re: better ad rotator

Posted: Sun Jun 24, 2012 8:24 pm
by requinix
Since your requirements aren't very specific,

Code: Select all

class AdFactory {

	protected $ads = array(
		//    used,  HTML
		array(false, "insert ad 1 here"),
		array(false, "insert ad 2 here")
	);
	protected $uniqueremaining;

	public function __construct() {
		$this->uniqueremaining = count($this->ads);
	}

	public function addAd($html) {
		$this->ads[] = array(false, $html);
		$this->uniqueremaining++;
	}

	public function getAd() {
		return $this->ads[mt_rand(0, count($this->ads) - 1)][1];
	}

	public function getUniqueAd($fallback = false) {
		if ($this->uniqueremaining == 0) {
			return ($fallback ? $this->getAd() : false);
		}
		$n = mt_rand(1, $this->uniqueremaining);
		foreach ($this->ads as $key => $ad) {
			if (!$ad[0] && --$n == 0) {
				$this->ads[$key][0] = true;
				return $ad[1];
			}
		}
	}

}

Re: better ad rotator

Posted: Sun Jul 01, 2012 5:09 pm
by Vegan
thanks that looks a lot better than what I have been able to figure out with PHP so far.

The idea I have is to say select some number of unique ads when constructing a page.

Games are plentiful compared to the amount of space in a page available. So selecting unique from the pool means at least a better chance of winning the viewer's interest?