• 0

[PHP] Advertisement rotator


Question

Is there a pre written, application for this, or just an easy way to do it.

I want to when loading a page have it randomly select an image with an accompanied URL.

So when you load the page you will get Image A and URL A however next time you load it you might get Image D and URL D?

This time i do have access to full PHP capabilities to host it.

Also if possible i need the ability to add prejudice to them so that one or more of them will load more often than others.

any help is appreciated thank you.

Link to comment
Share on other sites

19 answers to this question

Recommended Posts

  • 0

Sure, this is untested, but should work as expected. ;)

$advertisers = array(
	array(
 	'weighting' => 1,
 	'image' 	=> 'http://www.example.org/snowboarding/logo.jpg',
 	'url' 	=> 'http://www.example.org/snowboarding/'
	),
	array(
 	'weighting' => 3,
 	'image' 	=> 'http://www.example.org/skiing/logo.jpg',
 	'url' 	=> 'http://www.example.org/skiing/'
	),
);

$weighted_advertisers = array();

foreach($advertisers as $advertiser){
	for($i = 0; $i < $advertiser['weighting']; $i++){
 	array_push($weighted_advertisers, $advertiser);
	}
}

$random_weighted_advertiser = $weighted_advertisers[array_rand($weighted_advertisers)];

Link to comment
Share on other sites

  • 0

so i just put the <php bit on the beginning and end of that to create a .php file then in my source link to that php as the source image and it will randomly select something listed?

Link to comment
Share on other sites

  • 0

Say wha...? :huh:

Run the following bit of code and view the HTML (source) generated, you should see what happens. :cool:

&lt;?php
$advertisers = array(
	array(
 	'weighting' =&gt; 1,
 	'image' 	=&gt; 'http://www.example.org/snowboarding/logo.jpg',
 	'url' 	=&gt; 'http://www.example.org/snowboarding/'
	),
	array(
 	'weighting' =&gt; 3,
 	'image' 	=&gt; 'http://www.example.org/skiing/logo.jpg',
 	'url' 	=&gt; 'http://www.example.org/skiing/'
	),
);

$weighted_advertisers = array();

foreach($advertisers as $advertiser){
	for($i = 0; $i &lt; $advertiser['weighting']; $i++){
 	array_push($weighted_advertisers, $advertiser);
	}
}

$random_weighted_advertiser = $weighted_advertisers[array_rand($weighted_advertisers)];

printf(
	'&lt;a href="%s"&gt;
 	&lt;img src="%s" /&gt;
	&lt;/a&gt;',
	$random_weighted_advertiser['url'],
	$random_weighted_advertiser['image']
);

?&gt;

Link to comment
Share on other sites

  • 0

Yes it displays the image with a link. That works great, However, Do I create this as its own .php file or run it inline with the page :p

Right now i have the following code

&lt;div class="panel"&gt;
   &lt;div class="inner"&gt;&lt;span class="corners-top"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;div class="content"&gt;&lt;/br&gt;
         &lt;p style="text-align: center;"&gt;
           &lt;br/&gt; &lt;img src="location/file.php?image=.gif" /&gt;
         &lt;/p&gt;
      &lt;/div&gt;
   &lt;span class="corners-bottom"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;

I have also attempted just location/file.php

When i browse to location/file.php or file.php?image=.gif it will display however, in the code it does not display

Link to comment
Share on other sites

  • 0

&lt;div class="panel"&gt;
 &lt;div class="inner"&gt;&lt;span class="corners-top"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
 &lt;div class="content"&gt;&lt;/br&gt;
 &lt;p style="text-align: center;"&gt;
 &lt;?php include('path/to/script/i/posted.php'); ?&gt;
 &lt;/p&gt;
 &lt;/div&gt;
 &lt;span class="corners-bottom"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;

:cool:

Link to comment
Share on other sites

  • 0
When i browse to location/file.php or file.php?image=.gif it will display however, in the code it does not display

That happens because you need file.php to output image data instead of HTML. You want the PHP script to look up an advertisement to display, read the image file contents and output those contents. When you view file.php in a browser, it'll be rendered as HTML but what you really want is to make it output an image. Therefore you need to use file_get_contents() or fopen(), a header() call to change the content type to image/gif according to the file type and then output the cotents.

This approach works, but it's not the recommended way to do it. It's much better to output a random image URL in the src attribute rather than referencing another PHP file to output a random image - that's also what Anthony did in the previous post. The script only has to output the URL of the advertisement image which will can then be served without any PHP script (saves computing power on the server). Then, when the client receives an image URL it has loaded before, it can get the image from its cache and doesn't need to request it again (saves loading time on the client).

There are cases where you may prefer to go with the PHP script to read and output the image data. For example: when you want to dynamically generate an image every time an advertisement has to be displayed, you can use such script and use PHP's GD functions to draw on the image.

Link to comment
Share on other sites

  • 0

&lt;div class="panel"&gt;
 &lt;div class="inner"&gt;&lt;span class="corners-top"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
 &lt;div class="content"&gt;&lt;/br&gt;
 &lt;p style="text-align: center;"&gt;
 &lt;?php include('path/to/script/i/posted.php'); ?&gt;
 &lt;/p&gt;
 &lt;/div&gt;
 &lt;span class="corners-bottom"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;

:cool:

I attempted using this code, linking to my php file however, the image still does not display. For the sake of finally getting this to work i will post actual link information

&lt;div class="panel"&gt;
   &lt;div class="inner"&gt;&lt;span class="corners-top"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;div class="content"&gt;&lt;/br&gt;
         &lt;p style="text-align: center;"&gt;
           &lt;br/&gt; &lt;?php include('http://www2.golfworks.com/sponsor/Sponsor.php'); ?&gt;
         &lt;/p&gt;
      &lt;/div&gt;
   &lt;span class="corners-bottom"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;?php
$advertisers = array(
        array(
        'weighting' =&gt; 1,
        'image'         =&gt; 'http://www2.golfworks.com/sponsor/sponsor_1.gif',
        'url'   =&gt; 'http://www.golfworks.com/category.asp_Q_c_E_63_A_c2c_E_ls'
        ),
        array(
        'weighting' =&gt; 1,
        'image'         =&gt; 'http://www2.golfworks.com/sponsor/sponsor_2.gif',
        'url'   =&gt; 'http://www.golfworks.com/category.asp_Q_c_E_51_A_c2c_E_ls'
        ),
        array(
        'weighting' =&gt; 1,
        'image'         =&gt; 'http://www2.golfworks.com/sponsor/sponsor_3.gif',
        'url'   =&gt; 'http://www.golfworks.com/category.asp_Q_c_E_79_A_c2c_E_ls'
        ),
        array(
        'weighting' =&gt; 1,
        'image'         =&gt; 'http://www2.golfworks.com/sponsor/sponsor_4.gif',
        'url'   =&gt; 'http://www.golfworks.com/category.asp_Q_c_E_10070_A_c2c_E_ls'
        ),
        array(
        'weighting' =&gt; 1,
        'image'         =&gt; 'http://www2.golfworks.com/sponsor/sponsor_5.gif',
        'url'   =&gt; 'http://www.golfworks.com/category.asp_Q_c_E_80_A_c2c_E_ls'
        ),
        array(
        'weighting' =&gt; 3,
        'image'         =&gt; 'http://www2.golfworks.com/sponsor/sponsor_6.gif',
        'url'   =&gt; 'http://www.golfworks.com/category.asp_Q_c_E_2100_A_c2c_E_ls'
        ),
        array(
        'weighting' =&gt; 3,
        'image'         =&gt; 'http://www2.golfworks.com/sponsor/sponsor_7.gif',
        'url'   =&gt; 'http://www.golfworks.com/category.asp_Q_c_E_2096_A_c2c_E_ls'
        ),
);

$weighted_advertisers = array();

foreach($advertisers as $advertiser){
        for($i = 0; $i &lt;= $advertiser['weighting']; $i++){
        array_push($weighted_advertisers, $advertiser);
        }
}

$random_weighted_advertiser = $weighted_advertisers[array_rand($weighted_advertisers)];

printf(
        '&lt;a href="%s"&gt;
        &lt;img src="%s" /&gt;
        &lt;/a&gt;',
        $random_weighted_advertiser['url'],
        $random_weighted_advertiser['image']
);

?&gt;

Also here is a link to a page with the inline code on it.

LINK The image should appear in the Third box down from the top (second Light gray one just below the login button

Link to comment
Share on other sites

  • 0

Do you have error reporting enabled? Since you should have found a message regarding that include() line:

&lt;?php include('http://www2.golfworks.com/sponsor/Sponsor.php'); ?&gt;

include() paths are file paths on the server, not URLs. So assuming that your script runs on /forums/index.php and the sponsor script is at /sponsor/Sponsor.php, the correct path would be:

&lt;?php include('../sponsor/Sponsor.php'); ?&gt;

Link to comment
Share on other sites

  • 0

Do you have error reporting enabled? Since you should have found a message regarding that include() line:

&lt;?php include('http://www2.golfworks.com/sponsor/Sponsor.php'); ?&gt;

include() paths are file paths on the server, not URLs. So assuming that your script runs on /forums/index.php and the sponsor script is at /sponsor/Sponsor.php, the correct path would be:

&lt;?php include('../sponsor/Sponsor.php'); ?&gt;

That is very helpful information, I did not know that, However, it still does not work. I also attempted to create the folder inside the same location as the file calling it and changing it to

&lt;div class="panel"&gt;
   &lt;div class="inner"&gt;&lt;span class="corners-top"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;div class="content"&gt;&lt;/br&gt;
         &lt;p style="text-align: center;"&gt;
           &lt;br/&gt; &lt;?php include('/sponsor/Sponsor.php'); ?&gt;
         &lt;/p&gt;
      &lt;/div&gt;
   &lt;span class="corners-bottom"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;

So the locations are now

http://www2.golfworks.com/forum/styles/prosilver/template/viewtopic_body.html

http://www2.golfworks.com/forum/styles/prosilver/template/sponsor/Sponsor.php

However, that did not work either.

And yes, i updated the locations of the gif files.

Link to comment
Share on other sites

  • 0

I fixed the links. The first link will still not look correct due to it going to the html file and not the php render.

Link to comment
Share on other sites

  • 0

As you are including this inside a phpBB file you have to do it slightly differently. Got this from the phpBB Wiki

&lt;!-- INCLUDEPHP somefile.php --&gt;

This does not work either. :(

Link to comment
Share on other sites

  • 0

The wiki provided above states you have to enable php on a per-template basis, have you done this?

I have not, I did not know you had to do this.

Link to comment
Share on other sites

  • 0

There is an option in your phpBB ACP called Allow php in templates, it's under Security settings on the front page of the ACP. You need to change that to yes for this to work.

Link to comment
Share on other sites

  • 0

There is an option in your phpBB ACP called Allow php in templates, it's under Security settings on the front page of the ACP. You need to change that to yes for this to work.

That was already selected.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.