Solved

If Then Else Statement to Randomly Show 2 Ads

Posted on 2008-06-09
1
753 Views
Last Modified: 2013-12-25
I have on page on a site I am working on that is a PHP page.  At the top it has an ad spot that is rotating 2 different ads.  It is working by coming up with a random number, then depending on whether it is even or odd, it displays one of the two ads.

Here is the php code:

<? if(rand(0,10)<5){ ?>
  <script type="text/javascript">
      google_ad_client = "pub-29xxxxxxxx";
      google_ad_width = 120;
      google_ad_height = 600;
      google_ad_format = "120x600_as";
      google_ad_type = "text_image";
      google_ad_channel ="6xxxxxxxxx";
      google_color_bg = "353269";
      google_color_link = "FFFFFF";
      google_color_text = "e0e0e0";
      google_color_url = "b8b8b8";
</script>
  <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
  <? }else{ ?>
  <a href="http://www.someadsite.com" target="_blank"><img src="http://www.somewebsite.com/images/web-ad-someadsite.jpg" width="120" height="620" border="0" /></a>
  <? } ?>

I need to pull off something like this, but on a standard .html page.  Is there a way to do this using javascript or straight html?
0
Comment
Question by:TheShaner
1 Comment
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 21749412
Like this
  <script type="text/javascript">

if (Math.floor(Math.random()*2) ==0) { 

      google_ad_client = "pub-29xxxxxxxx";

      google_ad_width = 120;

      google_ad_height = 600;

      google_ad_format = "120x600_as";

      google_ad_type = "text_image";

      google_ad_channel ="6xxxxxxxxx";

      google_color_bg = "353269";

      google_color_link = "FFFFFF";

      google_color_text = "e0e0e0";

      google_color_url = "b8b8b8";

    document.write('<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"><\/script>');

  }else{ 

      document.write('<a href="http://www.someadsite.com" target="_blank"><img src="http://www.somewebsite.com/images/web-ad-someadsite.jpg" width="120" height="620" border="0" /></a>');

  }

</script>

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now