PDA

View Full Version : Random Affiliate



maverick
06-30-2006, 04:48 AM
is there a very simple and small script to randomly select an affiliate banner and rotate it everytime the page is loaded i found a few but they have like 50 lines of codes,i use to use one a few yrs ago that was just 5 lines and was a simple <ahref="link"><img src='imgurl"> with 3 lines ontop and 2 under.:o

djr33
06-30-2006, 04:59 AM
Rotating a banner each time the page loads is kinda bad. Make sure the filesize is small enough... or it will really slow down the users on slower connections.

You might want to use cookies/sessions to keep track of the one that user got and keep it the same through a whole session on your site.
(or maybe you need them to see all the choices as they browse around)

Could you use php? Not that hard in that, just don't know the code in JS.


top of your page:
<?php
$array[0] = "insert tags here";
$array[1] = "more here";
//add entries as you please
$thing = $array[rand(0,1)]; //note that the 1 is to be replaced with the highest number above in the array list.
?>

then in your content, put this:
<?php echo $thing; ?>

You could code something similar in javascript, but I don't know the codes right now... you could look them up. Use arrays (note the $ is only used in php... no need for it in javascript), use a random number, then just change somethign on the page to the random number entry of the array. That's it.

maverick
06-30-2006, 12:24 PM
well yeah i think i could use php just that i know more about JS,in this part here where would i put the image url and where would i put the linkmsomething like :
<?php
$array[0] = "imageurl.gif,http://linktogoto.com";
$array[1] = "more here";

And that session part sounds really interesting,how would i insert it into this script? i heard of them before and seen some use it but never though of addin it to the ratating banners,thxs for the help :p

Mav

Twey
06-30-2006, 12:45 PM
At the very top of your page, before anything else (including whitespace):
<?php
error_reporting(E_ALL);
session_start();

$image = array(
array(
'image' => 'imageurl.gif',
'link' => 'http://linktogoto.com'
),
array(
'image' => 'imageurl.png',
'link' => 'http://anotherlinktogoto.com'
),
array(
'image' => 'imageurl.jpg',
'link' => 'http://yetanotherlinktogoto.com'
)
);

if(!isset($_SESSION['curimg']))
$_SESSION['curimg'] = rand(0, count($image) - 1);

$image = $image[$_SESSION['curimg']];

$image = '<a href="' . $image['link'] . '"><img src="' . $image['image'] . '"></a>';
?>
Where you want the image to appear:

<?php echo($image); ?>That will keep the banner the same for each user who visits your site, as per djr33's idea.

djr33
06-30-2006, 07:18 PM
I believe sessions are only server side, or at least limited using javascript. (you could use a cookie instead, or something, I guess)

php is a good language to learn.
(And, for me, javascript is a good language to learn)
so.. I do understand where you're coming from.

confusio
07-02-2006, 02:50 AM
that seems like a great script idea to get the images catched in the cache,i tried Twey's script and it displays the image but only the first one then i reloaded several times and it doesn't make it rotate?

djr33
07-02-2006, 06:35 AM
twey's script DOESN'T rotate based on refreshing. That's the point of sessions.

It will keep the banner the same, so it doesn't need to reload each time and slow down transfers of the pages.

It will be random at the start of each visit, then stay the same throughout the visit.

Taking that out would be easy, but it's certainly worth considering... will be a LOT nicer for dialup users.

confusio
07-02-2006, 10:45 PM
oh ok but it will change once you close to browse and revist?then thats fine :) another thing,is it possible to open the link in a new windows maybe something like :

$image = array(
array(
'image' => 'imageurl.gif',
'link' => 'http://linktogoto.com' target='_blank' ?

Twey
07-02-2006, 10:51 PM
$image = '<a href="' . $image['link'] . '" target="_blank"><img src="' . $image['image'] . '"></a>';

confusio
07-11-2006, 10:38 PM
Hey,

I'v been using this script for a while and it works awesome but sometimes this error comes up:

Notice: Undefined index: curing in /home/.foobar/dexter/ads/rightad.php on line 195

Line 195 is $image = $image[$_SESSION['curimg']];

Or Notice: Undefined offset: 65 in /home/.foobar/dexter/ads/rightad.php on line 195

Does anyone know why? Thanks

Twey
07-11-2006, 10:57 PM
You mistyped it: it's "curimg" not "curing."