• This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

Display A Random Banner

The way I did my rotating banner/logo was to put the following code into a PHP file, in my case, I called it "Header.php"

Code:
<?php

$folder = './';

$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';

$img = null;

if (substr($folder,-1) != '/') {
  $folder = $folder.'/';
}

if (isset($_GET['img'])) {
  $imageInfo = pathinfo($_GET['img']);
  if (
      isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
      file_exists( $folder.$imageInfo['basename'] )
  ) {
      $img = $folder.$imageInfo['basename'];
  }
} else {
  $fileList = array();
  $handle = opendir($folder);
  while ( false !== ( $file = readdir($handle) ) ) {
      $file_info = pathinfo($file);
      if (
          isset( $extList[ strtolower( $file_info['extension'] ) ] )
      ) {
          $fileList[] = $file;
      }
  }
  closedir($handle);

  if (count($fileList) > 0) {
      $imageNumber = time() % count($fileList);
      $img = $folder.$fileList[$imageNumber];
  }
}

if ($img!=null) {
  $imageInfo = pathinfo($img);
  $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
  header ($contentType);
  readfile($img);
} else {
  if ( function_exists('imagecreate') ) {
      header ("Content-type: image/png");
      $im = @imagecreate (100, 100)
          or die ("Cannot initialize new GD image stream");
      $background_color = imagecolorallocate ($im, 255, 255, 255);
      $text_color = imagecolorallocate ($im, 0,0,0);
      imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
      imagepng ($im);
      imagedestroy($im);
  }
}

?>

I dumped that into a directory, along with all the images I want to rotate (in this case, \Styles\Header). I then went into AdminCP\Appearance\Style Properties\Header and Navigation\Settings and changed the Header Logo Image Path to \Styles\Header\header.php.

What I like about this, is I don't have to define any specific images, and that it will rotate various image types (GIF/JPG/PNG). What I've done is provide my user base with the PSD, let them make their own, and submit them. I then incorporate them into the rotation by just dumping them into the header directory.
 
Is it possible to add a different set of rotating banners to different nodes? I have been creating children styles of my default style with a different banner set in the ad_above_top_breadcrumb template of each one and then assigning that style to the node the banners are for, but we have like 30 boards and I can't even get it so save anymore. I tried adding a conditional but I couldn't get it to work.

So say I had 3 boards with 2 banners in each one, is it possible to show those banners, in their respective nodes only, with all of the code in the same template/style? I actually have about 25 in my main board area and there are like 5 other boards that get 3 or 4 each. Maybe this would be a good suggestion for an add-on.
 
Is it possible to add a different set of rotating banners to different nodes? I have been creating children styles of my default style with a different banner set in the ad_above_top_breadcrumb template of each one and then assigning that style to the node the banners are for, but we have like 30 boards and I can't even get it so save anymore. I tried adding a conditional but I couldn't get it to work.

So say I had 3 boards with 2 banners in each one, is it possible to show those banners, in their respective nodes only, with all of the code in the same template/style? I actually have about 25 in my main board area and there are like 5 other boards that get 3 or 4 each. Maybe this would be a good suggestion for an add-on.

Here is code for you. Note that with this code I have generalized the banner selection in the foreach loop. This allows you to specify different numbers of banners for each forum, and you no longer have to specify the total number of banners for the rand calculation:

Rich (BB code):
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>

<xen:if is="{$forumId} == 2">
	<xen:set var="$banners.1">banner one for forumId 2</xen:set>
	<xen:set var="$banners.2">banner two for forumId 2</xen:set>
	<xen:set var="$banners.3">banner three for forumId 2</xen:set>
	<xen:set var="$banners.4">banner four for forumId 2</xen:set>
<xen:elseif is="{$forumId} == 4" />
	<xen:set var="$banners.1">banner one for forumId 4</xen:set>
	<xen:set var="$banners.2">banner two for forumId 4</xen:set>
	<xen:set var="$banners.3">banner three for forumId 4</xen:set>
	<xen:set var="$banners.4">banner four for forumId 4</xen:set>
	<xen:set var="$banners.5">banner five for forumId 4</xen:set>
<xen:else />
	<xen:set var="$banners.1">banner one for all other forums and pages</xen:set>
	<xen:set var="$banners.2">banner two for all other forums and pages</xen:set>
	<xen:set var="$banners.3">banner three for all other forums and pages</xen:set>
</xen:if>

<xen:foreach loop="$banners" value="$curBanner" i="$i" count="$count">
	<xen:if is="!{$rand}">
		<xen:set var="$rand">{xen:calc '({$serverTime} % {$count}) + 1'}</xen:set>
	</xen:if>
	<xen:if is="{$i} == {$rand}">
		{xen:raw '$banners.{$rand}'}
	</xen:if>
</xen:foreach>

Add more elseif's to check more forumIds.

But before this will work you need to pass the node_id using xen:container. Edit these two templates:

Admin CP -> Appearance -> Templates
> forum_view
> thread_view


Add this line to the top of each template:

Code:
<xen:container var="$forumId">{$forum.node_id}</xen:container>
 
Here is code for you. Note that with this code I have generalized the banner selection in the foreach loop. This allows you to specify different numbers of banners for each forum, and you no longer have to specify the total number of banners for the rand calculation:
Thank you so much, this is just what I needed. We really appreciate the support you guys offer.
 
Instead of using Img src, what's the correct code to link it to a file where the forum itself is located, such as /forums/images/banner.png? I tried URL'(images/banner.png)'

I figured i'm simply doing something incorrect! It works perfect with img src but i'd rather it not link to another site.

Edit: got it to work through a different route. Still annoyed that users can copy my banner though, is there a way to void the link that allows them to save the image?
 
Edit: got it to work through a different route. Still annoyed that users can copy my banner though, is there a way to void the link that allows them to save the image?

There is no way to prevent it. No matter what method you use, the image must still be downloaded by the client which allows a user to save it.
 
Where it says banner one, banner two etc

Can I put a google adsense or chitika javascript ad code for each space?

or do I have to do it as a template?
 
Is there a way to set it so a user can prefer one banner to appear (without making a style for each banner) instead of it being random, and it being random for any user who does not have the setting set?
 
Here is code for you. Note that with this code I have generalized the banner selection in the foreach loop. This allows you to specify different numbers of banners for each forum, and you no longer have to specify the total number of banners for the rand calculation:

Rich (BB code):
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>

<xen:if is="{$forumId} == 2">
	<xen:set var="$banners.1">banner one for forumId 2</xen:set>
	<xen:set var="$banners.2">banner two for forumId 2</xen:set>
	<xen:set var="$banners.3">banner three for forumId 2</xen:set>
	<xen:set var="$banners.4">banner four for forumId 2</xen:set>
<xen:elseif is="{$forumId} == 4" />
	<xen:set var="$banners.1">banner one for forumId 4</xen:set>
	<xen:set var="$banners.2">banner two for forumId 4</xen:set>
	<xen:set var="$banners.3">banner three for forumId 4</xen:set>
	<xen:set var="$banners.4">banner four for forumId 4</xen:set>
	<xen:set var="$banners.5">banner five for forumId 4</xen:set>
<xen:else />
	<xen:set var="$banners.1">banner one for all other forums and pages</xen:set>
	<xen:set var="$banners.2">banner two for all other forums and pages</xen:set>
	<xen:set var="$banners.3">banner three for all other forums and pages</xen:set>
</xen:if>

<xen:foreach loop="$banners" value="$curBanner" i="$i" count="$count">
	<xen:if is="!{$rand}">
		<xen:set var="$rand">{xen:calc '({$serverTime} % {$count}) + 1'}</xen:set>
	</xen:if>
	<xen:if is="{$i} == {$rand}">
		{xen:raw '$banners.{$rand}'}
	</xen:if>
</xen:foreach>

Add more elseif's to check more forumIds.

But before this will work you need to pass the node_id using xen:container. Edit these two templates:

Admin CP -> Appearance -> Templates
> forum_view
> thread_view


Add this line to the top of each template:

Code:
<xen:container var="$forumId">{$forum.node_id}</xen:container>

In 1.1 this is possible with just the one big template edit. No longer do you need to add xen:container tags to pass the forumId. Just use this code and that's it:

Rich (BB code):
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>

<xen:if is="{$forum.node_id} == 2">
	<xen:set var="$banners.1">banner one for forumId 2</xen:set>
	<xen:set var="$banners.2">banner two for forumId 2</xen:set>
	<xen:set var="$banners.3">banner three for forumId 2</xen:set>
	<xen:set var="$banners.4">banner four for forumId 2</xen:set>
<xen:elseif is="{$forum.node_id} == 4" />
	<xen:set var="$banners.1">banner one for forumId 4</xen:set>
	<xen:set var="$banners.2">banner two for forumId 4</xen:set>
	<xen:set var="$banners.3">banner three for forumId 4</xen:set>
	<xen:set var="$banners.4">banner four for forumId 4</xen:set>
	<xen:set var="$banners.5">banner five for forumId 4</xen:set>
<xen:else />
	<xen:set var="$banners.1">banner one for all other forums and pages</xen:set>
	<xen:set var="$banners.2">banner two for all other forums and pages</xen:set>
	<xen:set var="$banners.3">banner three for all other forums and pages</xen:set>
</xen:if>

<xen:foreach loop="$banners" value="$curBanner" i="$i" count="$count">
	<xen:if is="!{$rand}">
		<xen:set var="$rand">{xen:calc '({$serverTime} % {$count}) + 1'}</xen:set>
	</xen:if>
	<xen:if is="{$i} == {$rand}">
		{xen:raw '$banners.{$rand}'}
	</xen:if>
</xen:foreach>
 
thanks, used this to randomize the header logo. worked perfect.

edited template - logo_block

here's the new template
Code:
<xen:set var="$banners.1">http://www.xxxxxxxxx/logo1.png</xen:set>
<xen:set var="$banners.2">http://www.xxxxxxxxx/logo2.png</xen:set>
<xen:set var="$banners.3">http://www.xxxxxxxxx/logo3.gif</xen:set>
 
<div id="logoBlock">
    <div class="pageWidth">
        <div class="pageContent">
            <xen:include template="ad_header" />
            <xen:hook name="header_logo">
            <div id="logo"><a href="{$logoLink}">
                <span><xen:comment>This span fixes IE vertical positioning</xen:comment></span>
                <img src="{xen:raw '$banners.{xen:calc '({$serverTime} % 3) + 1'}'}" alt="{$xenOptions.boardTitle}" />
            </a></div>
            </xen:hook>
            <span class="helper"></span>
        </div>
    </div>
</div>
 
I'm trying to add banners at the top right of the forum to be displayed all the time - whatever node people are looking at

Now I also want the banner to be clickable to go to a different site address

I thought I needed to edit ad_header

with this code

Code:
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>
 
<xen:set var="$banners.1"><a href="http://www.simonrevill.com/wedding-workflow-and-processing-1-to-1-photographer-training"><img src="http://www.ippn.co.uk/DFPpartners/ADbanner1.png" /></xen:set>
<xen:set var="$banners.2"><a href="http://www.simonrevill.com/wedding-workflow-and-processing-1-to-1-photographer-training"><img src="http://www.ippn.co.uk/DFPpartners/ADbanner1.png" /></xen:set>
<xen:set var="$banners.3"><a href="http://www.simonrevill.com/wedding-workflow-and-processing-1-to-1-photographer-training"><img src="http://www.ippn.co.uk/DFPpartners/ADbanner1.png" /></xen:set>
 
<xen:set var="$rand">{xen:calc '({$serverTime} % 3) + 1'}</xen:set>
 
{$banners.{$rand}}

But all I'm getting is a line of code <a to /> displayed above my forum logo on the left hand side ?
 
I'm trying to add banners at the top right of the forum to be displayed all the time - whatever node people are looking at

Now I also want the banner to be clickable to go to a different site address

I thought I needed to edit ad_header

with this code

Code:
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>
 
<xen:set var="$banners.1"><a href="http://www.simonrevill.com/wedding-workflow-and-processing-1-to-1-photographer-training"><img src="http://www.ippn.co.uk/DFPpartners/ADbanner1.png" /></xen:set>
<xen:set var="$banners.2"><a href="http://www.simonrevill.com/wedding-workflow-and-processing-1-to-1-photographer-training"><img src="http://www.ippn.co.uk/DFPpartners/ADbanner1.png" /></xen:set>
<xen:set var="$banners.3"><a href="http://www.simonrevill.com/wedding-workflow-and-processing-1-to-1-photographer-training"><img src="http://www.ippn.co.uk/DFPpartners/ADbanner1.png" /></xen:set>
 
<xen:set var="$rand">{xen:calc '({$serverTime} % 3) + 1'}</xen:set>
 
{$banners.{$rand}}

But all I'm getting is a line of code <a to /> displayed above my forum logo on the left hand side ?

See the first post:

http://xenforo.com/community/threads/display-a-random-banner.19563/

You need to use xen:raw to avoid the HTML characters being converted.

edit - and you are missing your closing </a> tags.
 
Thanks - got it displaying​
How do I get it to sit at the right hand side not in the middle​
Thanks for your help​
Code:
<xen:hook name="ad_header" />
 
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>
 
<xen:set var="$banners.1"><a href="http://www.simonrevill.com/wedding-workflow-and-processing-1-to-1-photographer-training"><img src="http://www.ippn.co.uk/DFPpartners/ADbanner1.png" /></a></xen:set>
<xen:set var="$banners.2"><a href="http://www.simonrevill.com/wedding-workflow-and-processing-1-to-1-photographer-training"><img src="http://www.ippn.co.uk/DFPpartners/ADbanner1.png" /></a></xen:set>
<xen:set var="$banners.3"><a href="http://www.simonrevill.com/wedding-workflow-and-processing-1-to-1-photographer-training"><img src="http://www.ippn.co.uk/DFPpartners/ADbanner1.png" /></a></xen:set>
 
{xen:raw '$banners.{xen:calc '({$serverTime} % 3) + 1'}'}
 
Top Bottom