I already did quite a few searches on these forums and I did not find what I was searching for so I decided to post my question.
I am trying to find a way to display a different ad in the same spot with same dimension, upon each refresh. I am using HTML for the web site and we have a few different codes for the ads. Here are two examples:
Javascript Code:
<script type="text/javascript"><!--
google_ad_client = "pub-8412003942395982";
/* 468x60, created 4/23/08 */
google_ad_slot = "6187880626";
google_ad_width = 468;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//-->
</script>
Javascript Code:
<script type="text/javascript">
AC_FL_RunContent('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0','width','160','height','600','src','http://www.Counter-Strike.com/affiliate/banners/cs-flash_160x600?actionURL=http://www.Counter-Strike.com/affiliate/idevaffiliate.php?id=2477_5_1_19','quality','high','pluginspage','http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash','movie','http://www.Counter-Strike.com/affiliate/banners/cs-flash_160x600?actionURL=http://www.Counter-Strike.com/affiliate/idevaffiliate.php?id=2477_5_1_19'); //end AC code
</script>
They seem to be different codes though they are both Java Scripts. I don't know how I would put about 7 of them on a rotation to display a new one on each page view/refresh. Can someone please help me?
Thanks!
Sincerely,
Jason H.
Last edited by UnrealEd; 05-03-08 at 04:32 PM.
Reason: please use the proper syntax highlighters
Sorry, you wanted Javascript.
I don't think it can be done with HTML or even with HTML & Javascript.
You need a server side scripting language, like PHP.
There is no way to store values in Javascript variables.
Because they get reset when the page is refreshed.
Now if you just wanted to rotate adds while viewing the page, then that can be done with Javascript.
I am totally ok with PHP. I am just less advanced with PHP thus I wanted to steer clear of it. If people are willing to walk me through or point me in the right direction, I am up for anything. Please continue with PHP!
I use a session variable to hold the rotation count and a SWITCH to do the rotating.
PHP Code:
<?php session_start(); $_SESSION["counter"]=!empty($_SESSION["counter"])?$_SESSION["counter"]:0; $_SESSION["counter"]++; switch($_SESSION["counter"]) { case 1: ?> <div> Place first add here </div> <?php break; case 2: ?> <div> Place second add here </div> <?php break; case 3: ?> <div> Place third add here </div> <?php break; default: ?> <div> Place last add here </div> <?php $_SESSION["counter"]=0; } ?>
You can add as many case's to the SWITCH as you need.
Just make sure the default: is at the end so it can reset the counter.
I have session_start(); at the top of this page. If you plan on putting this SWITCH somewhere in the middle of your HTML code, then put the session_start() at the very top of the page.
Like this:
Using an array makes the code even better: you can get rid of the entire switch statement, and makes it easier to add data if you want to use a database or flatfile in which you store the adds
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks
I'd agree in most circumstances, however as DieuxSoldat_04 quite clearly wants as little php code as possible because of lack of understanding, I think this is a great solution. It's simple, yet works perfectly for what he needs. If the file has say 400 ads, obviously you would change the method by which the ads are stored/retrieved such as a mysql db.
Thanks for the code! I haven't tried it yet but I am about to. Just to be sure, is the following code layout how I could do it?
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div class="sidecolumn">
<?php
session_start();
$_SESSION["counter"]=!empty($_SESSION["counter"])?$_SESSION["counter"]:0;
$_SESSION["counter"]++;
switch($_SESSION["counter"])
{
case 1:
?>
<div>
Place first add here
</div>
<?php
break;
case 2:
?>
<div>
Place second add here
</div>
<?php
break;
case 3:
?>
<div>
Place third add here
</div>
<?php
break;
default:
?>
<div>
Place last add here
</div>
<?php
$_SESSION["counter"]=0;
}
?>
</div>
</body>
</html>
Once I find out where exactly it is suppose to go, I will give it a shot : D Also just to be sure, I replace every "ad here" with a script like this one: