Page 1 of 2

new ad module

Posted: Fri Oct 05, 2012 12:47 pm
by Vegan
Given a game review has a lot of white space due to the way my site is currently designed, why not stuff that area with ads?

so I have some ads now

Code: Select all

<div style="width:500px; text-align:center" class="float-right">
<iframe src="http://rcm.amazon.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=B0062U5KG6" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
<iframe src="http://rcm.amazon.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=B0076DO5A0" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
<iframe src="http://rcm.amazon.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=B0053BSMPQ" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
<iframe src="http://rcm.amazon.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=B005THAX60" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
<iframe src="http://rcm.amazon.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=B002I0KOSI" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
<iframe src="http://rcm.amazon.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=B0050SZ0SY" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
<iframe src="http://rcm.amazon.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=B007FTE2VW" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
<iframe src="http://rcm.amazon.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=B0050SWTS8" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
and I have this class

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];
                        }
                }
        }

}
So what is the best strategy to select 8 ads for this block, assuming I can come up with a lot more game ads

Re: new ad module

Posted: Fri Oct 05, 2012 4:02 pm
by requinix
Call getUniqueAd() 8 times? Or modify it so that it can return more than one ad at a time.

Re: new ad module

Posted: Fri Oct 05, 2012 6:27 pm
by Christopher
How about using array_rand() ?

Re: new ad module

Posted: Sat Oct 06, 2012 8:34 am
by Vegan
i used to do it like this

$ads = array(
array(
'insert ad 1 here'
),
array(
'insert ad 2 here'
),
),
);

$ad = $ads[array_rand($ads)];
echo $ad;
?>

now I am trying to figure out how to do a bunch at a time for larger ad blocks

Re: new ad module

Posted: Sat Oct 06, 2012 5:43 pm
by requinix
Considering I wrote the AdFactory you have now, how about I add a getMultipleAds() method?

Code: Select all

        public function getMultipleAds($number) {
                $ads = array();
		foreach (array_rand($this->ads, $number) as $key) {
			$ads[] = $this->ads[$key][1];
		}
		return $ads;
        }
That doesn't respect any of the uniqueness stuff - something you might want to do...

Re: new ad module

Posted: Sun Oct 07, 2012 8:34 am
by Vegan
I prefer to have all different ads, for different games from a given pool.

Code: Select all

<?
class AdFactory {

        protected $ads = array(
                //    used,  HTML
                // make sure last one has the comma removed
                array(false, '<iframe src="http://rcm.amazon.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=B0062U5KG6" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B0076DO5A0" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B0053BSMPQ" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B005THAX60" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B002I0KOSI" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B0050SZ0SY" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B007FTE2VW" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B0050SWTS8" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>')
                // 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];
                        }
                }
        }

}
?>
<div style="width:500px; text-align:center" class="float-right">


</div>


Re: new ad module

Posted: Sun Oct 07, 2012 5:33 pm
by requinix
array_rand() will give you all unique ads... in that block. It doesn't guarantee that you'll have unique ads across all blocks.

Like I said, modify the code so that it respects uniqueness. Try to merge the stuff in getMultipleAds() with getUniqueAd().

Re: new ad module

Posted: Mon Oct 08, 2012 9:21 pm
by Vegan
what I wanted to know is it possible to use a C style for loop to grab n ads?

Code: Select all

<?
class AdFactory {

        protected $ads = array(
                //    used,  HTML
                // make sure last one has the comma removed
                array(false, '<iframe src="http://rcm.amazon.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=B0062U5KG6" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B0076DO5A0" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B0053BSMPQ" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B005THAX60" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B002I0KOSI" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B0050SZ0SY" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B007FTE2VW" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>'),
                array(false, '<iframe src="http://rcm.amazon.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=B0050SWTS8" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>')
                // 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];
                        }
                }
        }
        public function getMultipleAds($number) {
                $ads = array();
                foreach (array_rand($this->ads, $number) as $key) {
                        $ads[] = $this->ads[$key][1];
                }
                return $ads;
        }
}

// now its possible to conjure up the actual ad
$factory = new AdFactory;
$ad = $factory->getAd();
echo $ad

?>


Re: new ad module

Posted: Mon Oct 08, 2012 11:53 pm
by requinix
Vegan wrote:what I wanted to know is it possible to use a C style for loop to grab n ads?
I don't know what you mean.

Re: new ad module

Posted: Tue Oct 09, 2012 9:38 pm
by Vegan

Code: Select all

<div class="float-right" style="width: 500px; text-align: center">
<?
// now its possible to conjure up the actual ad
$factory = new AdFactory;
for ($i=0; i<8; i++) {
	$ad = $factory->getAd();
	echo $ad
}
?>
</div>


Re: new ad module

Posted: Tue Oct 09, 2012 10:22 pm
by requinix
Sure (but you probably want unique ads and should call getUniqueAd() instead). Which, if I may point it out, is the very first thing I suggested.

Re: new ad module

Posted: Wed Oct 10, 2012 1:30 pm
by Christopher
I agree with requinix. Maybe something like:

Code: Select all

$ads = $adModel->getUniqueAds(8);
for ($ads as $ad) {
	echo $ad
}

Re: new ad module

Posted: Wed Oct 10, 2012 7:16 pm
by Vegan
I will be adding a lot more ads before I put the module to the test.

With the get unique, the will prevent dupes

I want to thank everyone here for the help as now I am at lest now clearer about programming in PHP

later on after I am happy with my current coding I will be back for something different

Re: new ad module

Posted: Wed Oct 10, 2012 7:18 pm
by Vegan
Christopher wrote:I agree with requinix. Maybe something like:

Code: Select all

$ads = $adModel->getUniqueAds(8);
for ($ads as $ad) {
	echo $ad
}
can you show me how to do that, I was going to simply use a for loop

desired output

// canned div container
ad 1
ad 2
ad 3
...
// canned closing tag

Re: new ad module

Posted: Wed Oct 10, 2012 10:20 pm
by Christopher
So a simple way to think about this is:

Code: Select all

// Model
$ad = 'B0050SWTS8';

// View
<iframe src="http://rcm.amazon.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=<?php echo $ad; ?>" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
So you want a Model that provides some random picks out of a larger list of those data strings. So something like:

Code: Select all

class AdModel
{
    protected $data;

    public function __construct()
    {
        // this could also be passed a database connection and fetch the ads from a database here
        $this->data = array(
            'B0062U5KG6',
            'B0076DO5A0',
            'B0053BSMPQ',
            'B005THAX60',
            'B002I0KOSI',
            'B0050SZ0SY',
            'B007FTE2VW',
            'B0050SWTS8',
            'B001234ABC',
            'B001234DEF',
            'B001234GHI',
            'B001234XYZ',      // FYI - in PHP you can have a comma on the last item
            );
    }

    public function getUniqueAds($n)
    {
        $rand_keys = array_rand($this->data, $n);
        $ads = array();
        foreach ($rand_keys as $key) {
            $ads[] = $this->data[$key];
        }
        return $ads;
    }
}
And the View:

Code: Select all

<?php
$ads = $adModel->getUniqueAds(8);
foreach ($ads as $ad) {
?>
<iframe src="http://rcm.amazon.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=<?php echo $data; ?>" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
<?php
}
?>