Gossamer Forum
Home : Products : Links 2.0 : Customization :

A very simple random banner!

Quote Reply
A very simple random banner!
Just wanted something simple that changed the banner and link on each build. I was messing around thinking of using over complicated methods when all I really needed to do was this:

All I would use are about 5 diff banners so I put this into site_html_templates.pl:


in the globals:


rban => &rban,


then at the bottom add this sub routine (changing $count to the number of banners your using and changing the url's etc...:


sub rban {
# --------------------------------------------------------
my ($rando, $rand, $count);
$count = 5;

# Get a random banner on each build
$rand = int(rand ($count + 0.5));
if ($rand eq 1) {
$rando = "<a href=\"/url 1 here\"><IMG SRC="image 1 url here" ALT="image 1"></a>";
}
if ($rand eq 2) {
$rando = "<a href=\"/ url 2 here\"><IMG SRC="image 2 url here" ALT="image 2"></a>";
}
if ($rand eq 3) {
$rando = "<a href=\"/ url 3 here\"><IMG SRC="image 3 url here" ALT="image 3"></a>";
}
if ($rand eq 4) {
$rando = "<a href=\"/ url 4 here\"><IMG SRC="image 4 url here" ALT="image 4"></a>";
}
if ($rand eq 5) {
$rando = "<a href=\"/ url 5 here\"><IMG SRC="image 5 url here" ALT="image 5"></a>";
}
return $rando;
}


Then add <%rban%> wherever you want the banner to be displayed.


(Also I've added a few things to do the following: count the number of times each banner is displayed, count the number of clicks each banner gets, display them via graphs on a results template, I'll post the code for this soon)


Good Luck!

Glenn

http://mir.spaceports.com/~glennu/
Quote Reply
Re: A very simple random banner! In reply to
You might want to use elseif statements. Also actually for the last one you'd want to use an else statement instead, something like:

else ( $rando = "<a href=\"/ url 5 here\"><IMG SRC="image 5 url here" ALT="image 5"></a>"; )

You could use that for the banner you want to be displayed the most and then set the count a bit higher than the actual number of banners there are.



Good Luck!

Glenn

http://mir.spaceports.com/~glennu/
Quote Reply
Re: [glennu] A very simple random banner! In reply to
Hi

If i want to do a sub rban2 (i want to display different random items in one page)
sometimes i get a random result with rban2, sometimes no.

is there a contradiction with sub rban ?



here are my codes, thanks if you know.







sub rban {
# --------------------------------------------------------

my ($rando, $rand, $count);
$count = 5;

# Get a random banner on each build
$rand = int(rand ($count + 0.5));
if ($rand eq 1) {
$rando = "<a href=\"/http://mysite.net//\">TEST1</a>";
}
if ($rand eq 2) {
$rando = "<a href=\"/http://mysite.net//\">TEST2</a>";
}
if ($rand eq 3) {
$rando = "<a href=\"/http://mysite.net//\">TEST3</a>";
}
if ($rand eq 4) {
$rando = "<a href=\"/http://mysite.net//\">TEST4</a>";
}
if ($rand eq 5) {
$rando = "<a href=\"/http://mysite.net//\">TEST5</a>";
}
return $rando;
}

sub rban2 {
# --------------------------------------------------------

my ($rando2, $rand2, $count2);
$count2 = 5;

# Get a random banner on each build
$rand2 = int(rand ($count2 + 1.5));
if ($rand2 eq 1) {
$rando2 = "<a href=\"/http://mysite.net//\">TEST1 card</a>";
}
if ($rand2 eq 2) {
$rando2 = "<a href=\"/http://mysite.net//\">TEST2 card</a>";
}
if ($rand2 eq 3) {
$rando2 = "<a href=\"/http://mysite.net//\">TEST3 card</a>";
}
if ($rand2 eq 4) {
$rando2 = "<a href=\"/http://mysite.net//\">TEST4 card</a>";
}
if ($rand2 eq 5) {
$rando2 = "<a href=\"/http://mysite.net//\">TEST5 card</a>";
}
else {
$rando2 = "$rando2";
}
return $rando2;
}
Quote Reply
Re: [jigme] A very simple random banner! In reply to
What are all the slashes all over the place like / //\

There are problems with both those sub-routines. "eq" must not be used for number comparisons, you should use ==

The suggested code would be:

Code:
sub rban {
#--------------------------------------------------------

my $count = 5;
my $rand = int(rand ($count + 0.5));

return qq|<a href="http://mysite.net">TEST$rand</a>| if ($rand and $rand < 6);
}

Last edited by:

Paul: Oct 18, 2002, 9:43 AM