Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macaddict23

macrumors 6502
Original poster
Jun 20, 2006
382
1
MacVille, USA
I have four different ads that I would like shown on the left-hand side of my homepage. I want a different ad to show up each time my homepage refreshes, or each time a user visits the page. The ad is basically a 72x72px image with about 8 lines of HTML text wrapped around it. How can I make this happen? Thanks in advance!
 
ok so put the following code in you page where you would like the banners to show.

Code:
<Script src="banners.js" type="text/javascript" language="javascript"></script>

next create an html page for each of your banner ads with the picture and text layed out as you want them.

then download the attached javascipt file and open it up, use the comments to help you configure it.

then upload your the lot... should work :)

(adapted from http://www.dynamicdrive.com/dynamicindex17/randomiframe.htm)
 

Attachments

  • banners.js.zip
    1 KB · Views: 84
php?

I'd probably use some php to generate a number on load and then use that variable in the path of a php include file, which would have your banner code inside.

I'm not a huge php buff, but you could do something like this:
Put this above the doctype.
Code:
<?php
$includes_folder = "../includes";
$include_name = not really sure of that part, check out a php site
?>
then where you put your banner, use an include thing.
Code:
<?php include("$includes_folder/$include_name"); ?>
 
Here's the question: Does it need to rotate while the site is open, or just on every load? (Hint: it is much easier to do it on every page load)
 
Here's the question: Does it need to rotate while the site is open, or just on every load? (Hint: it is much easier to do it on every page load)

No, it does not have to rotate while the site is open. So yes, I want a new ad to rotate on every page load. Thanks!
 
OK, so assuming you have php turned on, simply put this code in your site where you want the banner to show up.
Code:
<?php
$therandom = rand(1, 7);
if ($therandom < 5){
echo "<img src=\"BannerThatShowsUp4xAsOftenURL\">";
} else if ($therandom == 5){
echo "<img src=\"SecondBannerURL\">";
} else if ($therandom == 6){
echo "<img src=\"ThirdBannerURL\">";
} else if ($therandom == 7){
echo "<img src=\"FourthBannerURL\">";
} else {
echo "the virus messed up!";
}
?>
this is untested so tell me if you have any troubles
 
OK, so assuming you have php turned on, simply put this code in your site where you want the banner to show up.
Code:
<?php
$therandom = rand(1, 7);
if ($therandom < 5){
echo "<img src=\"BannerThatShowsUp4xAsOftenURL\">";
} else if ($therandom == 5){
echo "<img src=\"SecondBannerURL\">";
} else if ($therandom == 6){
echo "<img src=\"ThirdBannerURL\">";
} else if ($therandom == 7){
echo "<img src=\"FourthBannerURL\">";
} else {
echo "the virus messed up!";
}
?>
this is untested so tell me if you have any troubles

word on the street is that mt_rand() is a much faster more efficient algorithm so id opt for this slightly diffent way:
Code:
<?php
$therandom = mt_rand(1, 7);
if ($therandom < 5){
echo "<img src=\"BannerThatShowsUp4xAsOftenURL\">";
} else if ($therandom == 5){
echo "<img src=\"SecondBannerURL\">";
} else if ($therandom == 6){
echo "<img src=\"ThirdBannerURL\">";
} else if ($therandom == 7){
echo "<img src=\"FourthBannerURL\">";
} else {
echo "error in random function";
}
?>

nice work tho :D
 
word on the street is that mt_rand() is a much faster more efficient algorithm so id opt for this slightly diffent way:
Code:
<?php
$therandom = mt_rand(1, 7);
if ($therandom < 5){
echo "<img src=\"BannerThatShowsUp4xAsOftenURL\">";
} else if ($therandom == 5){
echo "<img src=\"SecondBannerURL\">";
} else if ($therandom == 6){
echo "<img src=\"ThirdBannerURL\">";
} else if ($therandom == 7){
echo "<img src=\"FourthBannerURL\">";
} else {
echo "error in random function";
}
?>

nice work tho :D
Thanks. I'll use that next time.

I myself am relatively new to php. In fact, that was my first time using any random function at all.
 
I have four different ads that I would like shown on the left-hand side of my homepage. I want a different ad to show up each time my homepage refreshes, or each time a user visits the page. The ad is basically a 72x72px image with about 8 lines of HTML text wrapped around it. How can I make this happen? Thanks in advance!

You don't need PHP for that, JavaScript is sufficient:

<html>
<head>
<script language = "JavaScript">
<!--
function choose () {
if (document.images) {
var z = Math.floor(Math.random()*2);
switch (z)
{
case 0:
document["myimg"].src = "pics/pic0.jpg";
break;
case 1:
document["myimg"].src = "pics/pic1.jpg";
break;
}
}
}
-->
</script>
<body onload="choose()">
<img name="myimg" src="pics/nopic.jpg">
</body>
</html>

pics/pic0.jpg and pics/pic1.jpg are your ads, and pics/nopic.jpg can be a placeholder (e.g., a white pixel picture). When the page is loaded, the placeholder is replaced by a random picture (adjust the Math.random()*2 and add new switch clauses if you want more than 2 pictures). Sorry for the lack of indentation, I was too lazy.
 
You don't need PHP for that, JavaScript is sufficient:

<html>
<head>
<script language = "JavaScript">
<!--
function choose () {
if (document.images) {
var z = Math.floor(Math.random()*2);
switch (z)
{
case 0:
document["myimg"].src = "pics/pic0.jpg";
break;
case 1:
document["myimg"].src = "pics/pic1.jpg";
break;
}
}
}
-->
</script>
<body onload="choose()">
<img name="myimg" src="pics/nopic.jpg">
</body>
</html>

pics/pic0.jpg and pics/pic1.jpg are your ads, and pics/nopic.jpg can be a placeholder (e.g., a white pixel picture). When the page is loaded, the placeholder is replaced by a random picture (adjust the Math.random()*2 and add new switch clauses if you want more than 2 pictures). Sorry for the lack of indentation, I was too lazy.

It would work that way, but:
-PHP is more simple
-PHP is non client based
-Harder to define probabilities with your script
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.