Solved

If Then Else Statement to Randomly Show 2 Ads

Posted on 2008-06-09
1
757 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

777 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