SimplePortal

Customization => Blocks and Modifications => Block Requests => Topic started by: Verso on April 21, 2009, 04:06:36 AM

Title: Random banner block
Post by: Verso on April 21, 2009, 04:06:36 AM
I am looking for a block that will show a random banner.


Any helpers?
Title: Re: Random banner block
Post by: Nathaniel on April 21, 2009, 06:46:37 AM
That should be easy to do with a PHP block. Below is some example code that you could use.

Code: [Select]
$banners = array(
'http://au.php.net/images/php.gif',
'',
'',
);

echo '<img src="', $banners[rand(0, count($banners) - 1)], '" alt="" />';

The banners array can hold a list of images (banners) that will be shown randomly.
Title: Re: Random banner block
Post by: Verso on April 21, 2009, 08:01:03 AM
Thanks for your input but my coding knowledge is next to nothing......


The 3 logos I would like to be included are:-

http://www.suffolksportsforum/forumimages/fss.jpg

http://www.suffolksportsforum/forumimages/coastal.jpg

http://www.suffolksportsforum/forumimages/premier.gif



Could you amend the code for me please?


Title: Re: Random banner block
Post by: Nathaniel on April 21, 2009, 08:06:32 AM
Code: [Select]
$banners = array(
   'http://www.suffolksportsforum.com/forumimages/fss.jpg',
   'http://www.suffolksportsforum.com/forumimages/coastal.jpg',
   'http://www.suffolksportsforum.com/forumimages/premier.gif',
);

echo '<img src="', $banners[rand(0, count($banners) - 1)], '" alt="" />';
Title: Re: Random banner block
Post by: Verso on April 21, 2009, 08:18:51 AM
Sorry to be a nuisance but the block just appears empty
Title: Re: Random banner block
Post by: Eliana Tamerin on April 21, 2009, 10:42:22 AM
That's because you forgot the .co.uk on every one of those images. Here's what you need:

Code: [Select]
$banners = array(
   'http://www.suffolksportsforum.co.uk/forumimages/fss.jpg',
   'http://www.suffolksportsforum.co.uk/forumimages/coastal.jpg',
   'http://www.suffolksportsforum.co.uk/forumimages/premier.gif',
);

echo '<img src="', $banners[rand(0, count($banners) - 1)], '" alt="" />';
Title: Re: Random banner block
Post by: Verso on April 21, 2009, 10:53:42 AM
That's because you forgot the .co.uk on every one of those images. Here's what you need:

Code: [Select]
$banners = array(
   'http://www.suffolksportsforum.co.uk/forumimages/fss.jpg',
   'http://www.suffolksportsforum.co.uk/forumimages/coastal.jpg',
   'http://www.suffolksportsforum.co.uk/forumimages/premier.gif',
);

echo '<img src="', $banners[rand(0, count($banners) - 1)], '" alt="" />';
[/quote



How silly do I feel! putting .com instead of .co.uk!!!!


Thanks to both of you for your help - One last thing, can I add hyperlinks to the banners? and how..........
Title: Re: Random banner block
Post by: Nathaniel on April 21, 2009, 07:41:30 PM
That should be easy, if you want the same link for every banner then use this code:
Code: [Select]
$banners = array(
   'http://www.suffolksportsforum.co.uk/forumimages/fss.jpg',
   'http://www.suffolksportsforum.co.uk/forumimages/coastal.jpg',
   'http://www.suffolksportsforum.co.uk/forumimages/premier.gif',
);

echo '<a href="http://www.suffolksportsforum.co.uk"><img src="', $banners[rand(0, count($banners) - 1)], '" alt="" /></a>';

If you want a different link for each banner then use this code:
Code: [Select]
$banners = array(
  array('http://www.suffolksportsforum.co.uk/forumimages/fss.jpg', 'http://www.suffolksportsforum.co.uk'),
  array('http://www.suffolksportsforum.co.uk/forumimages/coastal.jpg', 'http://www.suffolksportsforum.co.uk'),
   array('http://www.suffolksportsforum.co.uk/forumimages/premier.gif', 'http://www.suffolksportsforum.co.uk'),
);

$rand = rand(0, count($banners) - 1);
echo '<a href="', $banners[$rand][1], '"><img src="', $banners[$rand][0], '" alt="" /></a>';

You can change the 'http://www.suffolksportsforum.co.uk' url to alter the location that they link to. :)
Title: Re: Random banner block
Post by: willerby on April 22, 2009, 03:03:26 AM
Thanks for this code LHVWB, really useful in so many ways.

Simple to amend for special offers, random forum links from a subset of posts chosen by admin as well as ads.

 8)
Title: Re: Random banner block
Post by: Verso on April 22, 2009, 04:14:18 AM
Yeah it's quality.


Thanks very much for the help. One more quick question though:-

Can I add any text to this block and (oh thats two questions) can these links open in another page?
Title: Re: Random banner block
Post by: willerby on April 24, 2009, 07:37:02 AM
If you want a different link for each banner AND open in a new window then use this code:

Code: [Select]
$banners = array(
  array('http://www.suffolksportsforum.co.uk/forumimages/fss.jpg', 'http://www.suffolksportsforum.co.uk'),
  array('http://www.suffolksportsforum.co.uk/forumimages/coastal.jpg', 'http://www.suffolksportsforum.co.uk'),
   array('http://www.suffolksportsforum.co.uk/forumimages/premier.gif', 'http://www.suffolksportsforum.co.uk'),
);

$rand = rand(0, count($banners) - 1);
echo '<a href="', $banners[$rand][1], '" target="_blank"><img src="', $banners[$rand][0], '" alt="" /></a>';
Title: Re: Random banner block
Post by: dougsbrat on April 24, 2009, 09:43:40 AM
THANKS for a great block!  ;D
Title: Re: Random banner block
Post by: dougsbrat on June 06, 2009, 07:21:48 PM
Updated SP 2.2.2
now "Random Banner" shows nothing
 :dead:
Title: Re: Random banner block
Post by: geekfairy on September 07, 2009, 02:59:07 AM
This works in the preview, but when I try to add the block it tells me there is a syntax error.

Any ideas what is causing it?

Thanks  :)
Title: Re: Random banner block
Post by: geekfairy on September 10, 2009, 11:02:00 PM
Nobody knows the answer?

Hmm, I don't understand why it works in preview but won't let me add the block :/
Title: Re: Random banner block
Post by: Nathaniel on September 11, 2009, 02:05:58 AM
Possibly an issue with the PHP syntax checking, try turning on the 'Disable PHP Validation' general setting.

Also, could you please post the exact code you are using? If there is an issue with the PHP syntax validation, then it would be quite useful.
Title: Re: Random banner block
Post by: geekfairy on September 11, 2009, 02:25:07 AM
Thanks, that did the trick.

Sorry, I didn't post the code because it was the same as the one above.

Thankyou :)
Title: Re: Random banner block
Post by: Nathaniel on September 26, 2009, 10:32:30 PM
geekfairy,
Could you please post or send me (via PM) the exact code that you use in your PHP block? The code above returns no issues for me with SP 2.3 and SMF 1.1.10 or SMF 2RC1.2.
Title: Re: Random banner block
Post by: jimsz on December 31, 2009, 10:22:52 AM
This code works great for a rotating banner but I have a couple questions - I'd like the image centered and any code I add results in a syntax error.

Also, I'd like to add some static text to the same block but that too results into a syntax error.
Title: Re: Random banner block
Post by: MultiformeIngegno on February 06, 2010, 12:12:13 PM
Is it possibile to have for each image a different caption text?
For example under the first image I'd like to have "First Image" (after a <br />), then under the second "Secondo Image", and so on... :)
Title: Re: Random banner block
Post by: haito on March 22, 2010, 02:13:52 PM
Is it possibile to have for each image a different caption text?
For example under the first image I'd like to have "First Image" (after a <br />), then under the second "Secondo Image", and so on... :)
Title: Re: Random banner block
Post by: Stigmartyr on March 23, 2010, 03:14:09 AM
I thought I might share a nifty banner rotator script I use.  You just drop all your images in a folder, and put a single php file in there with the images.  Then you call the php file as if it were an image and it rotates everything in that folder randomly.

I've attached the file below.


If this doesn't make sense, you just create a folder, lets call it banners.

You just put all your image files in /banners

Put the BannerRotator.php file in /banners and then call /banners/BannerRotator.php where you would call the image (i.e. <img src="/BannerRotator.php" )  works with BBC too.
Title: Re: Random banner block
Post by: Old Fossil on May 02, 2010, 11:42:31 AM
Hey stigmartyr

Any chance of having a peep at ya forum so we can see what it should look like?
Title: Re: Random banner block
Post by: Stigmartyr on May 13, 2010, 02:57:09 AM
Hey stigmartyr

Any chance of having a peep at ya forum so we can see what it should look like?

Sorry about taking a week to get back to you!

Here it is: http://stangnation.com/

It's the header banner that rotates on refresh. 

I've removed as much flash as I possible could in favor of jQuery
SimplePortal 2.3.8 © 2008-2024, SimplePortal