Solved

If Then Else Statement to Randomly Show 2 Ads

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
drop down navigation on mobile devices adds spaces 3 77
work allocation; web development; vba; access; 4 61
remove border from element 5 52
Animated .jpg? 13 59
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…
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The viewer will learn how to dynamically set the form action using jQuery.
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.

911 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

16 Experts available now in Live!

Get 1:1 Help Now