[TUTORIAL] Rotative Banners in PHP

DarkDragonLord

New Member
Messages
782
Reaction score
0
Points
0
Greetings everyone :D

Well, here its a fully commented PHP Script that allows you to create a rotative banner and yet, set up the href/link to the website.

I did not made it but i did translated all comments since were in PORTUGUESE.

PHP:
<?php
$banners = array('http://ddl.exofire.net/images/banner2.gif','http://ddl.exofire.net/images/468x80_banner.gif');

/* here is where is created the variable array type where you will be inserting the name of the images. FULL URL, */

$totalbanners = count($banners);

/* Here we use the count instruction to check the number of images inside the variable $banners */

$totalbanners--;

/* here we make a recount of the total number returned by the count, because an array starts with index 0 */

$randombanners = rand(0,$totalbanners);

/* the rand instruction ramdomly picks one image number generated by the count */

$link = array('www.google.com.br','ddl.exofire.net');

/* variable array type, that we define what is the URL of the HREF in the images. you must put in same order of the images.. NO NEED the http:/ because in the echo, we already add it. */

$alts = array('its google lol','my website');


echo "<a href=\"http://$link[$randombanners]\">
<img border=\"0\" width=\"yy\" height=\"xx\" src=\"$banners[$randombanners]\" alt=\"$alts[$randombanners]\">
</a>";

// ok, here we show the image ;P try it pressing F5 in your webpage ^^

?>

Remember to set the width and height, this way, if you just want 468x80 banners, if any "smart" one changes it banner to a 1024x768 to **** your webpage, it will not show full size, just the size you setup'ed


And i added the alt array just now so, i think will work perfectly, if not, let me know.
Credits:
http://www.baboo.com.br/absolutenm/templates/content.asp?articleid=5220&zoneid=24&resumo=
 
Last edited:

engel

New Member
Messages
54
Reaction score
0
Points
0
I have an alternative method to doing this:
(Before running the script, insert a banner code, one per line, into a file called banners.txt.
PHP:
<?php

function display_banners() {
	$banners = file("banners.txt");
	shuffle($banners);
	echo $banners[0];
}

?>
Then just call display_banners(); wherever you want to display it.
 

Thewinator

New Member
Messages
256
Reaction score
0
Points
0
I have to say that DDL's one could use a function as well to easily place it where you want it to.
But engels solution to put it in a txt file isn't the way to go.
If anyone knows the url of the txt file they could access it and that looks bad.
Also the method DDL used is 'Fool-proof' so to speak.
While engels could for instance be riged with:
<meta http-equiv="refresh" content="0;URL=google.com" />
 

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
Hi! I have the Ad management mod on my phpBB 3 and I want to add rotative banners in it. Apparently, I know putting php in an html document never works so how should I do it?
 

Thewinator

New Member
Messages
256
Reaction score
0
Points
0
Well that depends on how your mod works, try reading through its php files and look for the method that places the images, then just replace it with a div and an include within that div
 
Last edited:

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
Well that depends on how your mod works, try reading through its php files and look for the method that places the images, then just replace it with a div and an include within that div

The mod hasn't any php... Only html...
 

DarkDragonLord

New Member
Messages
782
Reaction score
0
Points
0
after you place my script, just change the html extension to .php

if stay in .html, server will not read the <?php tags :p
 

DarkDragonLord

New Member
Messages
782
Reaction score
0
Points
0
.
JUST AN ADD-ON:


If you want to add this to everypage of your website, instead of copying/pasting the code on every page, just do a new .php page and paste the code there. Add all things you want and save. NO NEED to put html tags or anything, just put the <?php and the codes, close with ?> and its done.

Then, in the place you want to add the code in the pages, put:

Code:
<?php
@include("[b]NAME OF THE PHP FILE WITH CODES[/b].php");
?>

Then, everytime that you need to change the code, instead of changing in all files, just change the php with the code.
AND, the @ before the include is, to when the file you including does not exist, instead of givin that PHP ERROR: include in line xx error etcs and show everyone your acc name (root/XXXX/public_html/etc) , just show nothing. ^^
 

deadimp

New Member
Messages
249
Reaction score
0
Points
0
Got bored, don't want to do my homework... Here's a compact version:
PHP:
//Store data in multidimensional array with associative indexes
$banner=array(
	array(
		'img'=>'http://ddl.exofire.net/images/banner2.gif',
		'url'=>'www.google.com.br',
		'alt'=>'its google lol'),
	array(
		'img'=>'http://ddl.exofire.net/images/468x80_banner.gif',
		'url'=>'ddl.exofire.net',
		'alt'=>'my website')
	);
//Generate random index
$index=rand(0,count($banner)-1);
//Select and reference current banner
$cur=&$banner[$index];
//Generate output
$out="<a href='$cur[url]'><img src='$cur[img]' alt='$cur[alt]'></a>";
Using just one main array allows the related data to be closer so it isn't so spread out.
I didn't test it, but it ought to work. Be sure you do something with $out.
 
Last edited:

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
Aside from adding border='0' to the image and inserting a title tag into the href, that pretty much sums it up :)

YOu could still use shuffle if you like though.
 
Top