Forum Moderators: coopster
I have a quick question and I don't know if it's possible, but currently I have a lot of visitors on my website. But they obviously know where the ads are and are avoiding them. Instead of increasing the amount of Adsense-advertisements (not very user friendly), I want the single Adsense-advertisement (yes I am using only one Adsense-advertisement on my website) to show up on random places, instead on the same place.
Is this possible? And can someone give me a piece of PHP code which does that? Cause I do not know much about PHP to do this myself.
I would be very grateful if someone could help me out, or give me a good example so I can do it myself.
/edit
In case it wasn't clear: I still want to have only 1 advertisement at a time on my site, but at different locations on my website.
[edited by: MvdL79 at 1:55 pm (utc) on Feb. 11, 2008]
<?
$max = 3;
$num = rand(1, $max);
if ($num == 1) {
?>
Insert page/ad version 1 here
<?
}
if ($num == 2) {
?>
Insert page/ad version 2 here
<?
}
if ($num == 3) {
?>
Insert page/ad version 3 here
<?
}
?>
Since you only want 1 ad per page. I would make three different pages and insert each version in the code where it says "Insert page/ad version 1/2/3 here". Then the randomizer will select one of the three versions of the page and display that.
I am trying to put it in here:
function addMenu()
{
echo '<ul class="menu">';
echo '<li><a class="menu" href="' . $this->m_BaseUrl . 'index.php">News</a></li>';
echo '<li><a class="menu" href="' . $this->m_BaseUrl . 'intro.php">Intro</a></li>';etc...
While the code itself works in a clean .php page, so I am guessing I am doing something wrong here...
I'm not sure where you tried to insert the random number generator but here are my thoughts
- decide how many places you want to use
- then you can generate a number 1 to 3,4,5 whatever
- generate that number at the very top of that file so it is global in scope
good ref
Understanding Custom Functions and Variable Scope in PHP [webmasterworld.com]
inside your functions you can then compare that variable and decide whether to output the adblock there
if you decided that 3 meant it would echo in the menu, the following code could go into that addMenu function
if ($randnum == 3) {
// paste all your adsense code for the menu here
}
then if you decided the header was 2 then yoiu could post another chunk similar to the above in your addHeader but that compared to 2
if ($randnum == 2) {
// paste all your adsense code for the header here
}
make any sense?
Now I tried several things, resulting in:
a) Showing nothing aka not working (25%)
or
b) Giving parse errors (75%)
I did take a look at that link you supplied, but still no succes. Maybe I am to stupid (100%). :(
I have just one add, which should appear (only once) in different locations in the menu. E.g. sometimes in the top of the menu, sometimes in the middle and sometimes at the end.
:(
Anyways, I got parse errors, unexpected ; " errors, etc.
And when everything was finally okay (no errors), it didn't show any ads on the website. :(
Normally, since I don't know much about PHP, I try and I try... Eventually I manage to get it working, but not this time. :(
start by putting your three ad blocks in there, no php code, just get the blocks working
next add a dummy variable at the top of your script, the very top
$myrandvar = 0;
next, add an if statement around only 1 block on your page, like so
if ($myrandvar == 1) {
echo '
<script type="text/javascript"><!--
google_ad_client = "pub-123456";
google_ad_width = 125;
google_ad_height = 125;
google_ad_format = "125x125_as";
google_ad_type = "text";
google_ad_channel = "123456";
google_color_border = "810100";
google_color_bg = "810100";
google_color_link = "FFFFFF";
google_color_text = "FFFFFF";
google_color_url = "FFFFFF";
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
';
}
that's it, now that block should disappear. The next thing to do is switch the value of your top variable to $myrandvar = 0; and then reload the page. Since the value at the top now matches, that ad block should now show.
You can then continue on to the next block, though make it a different value in he if statement, preferably 2, then 3 for the next, repeating the testing steps along the way.
Anyways I put it right under: function addMenu()
And then the ads showed up. Then I used the PHP that randomizes stuff and it works now!
Thank you guys very much!
Regards