php ad rotator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

php ad rotator

Post by Vegan »

been slowly figuring how to convert old code to PHP

so far i have this function more or less looking right???

Code: Select all

<?php 
$asin = array[];
array_push($asin, "B00UXTN5P0"); // GTX Titan X SC
// add more as needed

function showAISN($choice) {
	$temp = '<iframe src="https://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=cpa07-20&o=1&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=';
	$temp = $temp . $choice;
	$temp = $temp . '" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>';
	echo($temp);
    echo("\n");
?>
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php ad rotator

Post by Celauran »

I suppose that works. I probably would have used a partial and just passed the variable into it.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: php ad rotator

Post by Christopher »

Why call a function to add to the array? Are they coming from a datasource?

Code: Select all

$asin = array[];
array_push($asin, "B00UXTN5P0"); // GTX Titan X SC
// add more as needed
Why not just:

Code: Select all

$asin = array(
     'B00UXTN5P0', // GTX Titan X SC
     // add more as needed
     );
I am confused how the the array above works with the function? Do you select a random item from the array and pass it to the function?

I'd rather see Presentation functions return a string that I can in turn insert into another template:

Code: Select all

function buildAISN($choice) {
	$temp = '<iframe src="https://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=cpa07-20&o=1&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=';
	$temp = $temp . $choice;
	$temp = $temp . '" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>';
	 return $temp . "\n";
}

$str = buildAISN($aisn[array_rand($aisn)]);
Creating a class might be even cleaner (and autoloadable). Plus you could add other optional settings as needed without changing the interface to build().

Code: Select all

class Aisn
{
    private $ads;
    private $width = 120;
    private $height = 240;

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

    function setSize($width, $height)
    {
        $this->width = $width;
        $this->height = $height;
    }

    function build()
    {
	$temp = '<iframe src="https://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=cpa07-20&o=1&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=';
	$temp .= $this->ads[array_rand($$this->ads)]);
	$temp .= $temp . '" style="width:' . $this->width . 'px;height:' . $this->height . 'px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>';
	return $temp . "\n";
    )
}

$ad = new Aisn($aisnStandard);
$str = $ad->build();

$ad = new Aisn($aisnLarge);
$ad->setSize(160, 600);
$str = $ad->build();
(#10850)
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: php ad rotator

Post by Vegan »

the array is going to be used for a the rotator but I am not done with that yet

the rest is more just a conversion from how i was using javascript before

the idea was to use this PHP code as an alternative

I am not sure what all Amazon is doing with their new native ads, but this part of my code is more for a spot ad or 4 to fill white space etc

my code was intended to show a specific item, the randomized class looks useful for a swarm of randomized adds

clearly there is more to PHP to keep me puzzling away
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: php ad rotator

Post by Vegan »

eventually I may modify the showAISN once I am able to figure out of it works with Amazon outside of the US

using my location function i can hopefully be more global in my offerings
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
Post Reply