Please Help! Rotate Banners w/ no database!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arlene
Forum Newbie
Posts: 4
Joined: Fri Mar 14, 2003 3:42 pm

Please Help! Rotate Banners w/ no database!

Post by arlene »

Hello Everyone,

I am very new to PHP, but I really need help with this.

I have a site in php that has include files (i.e. "header.inc" left_side.inc")

I have specific banners in my image directory that I would like to rotate on specific pages only. The problem is that the banners display on the include files so if I want to rotate a banner for a toy company on my "kidstoys.php" page, how can I let the script know to only rotate
the banners I want? I don't have a database so I was wondering if their was a way I could get the path name from the browser and say something like:

{ if path="/kidtoys.gif" then display....

is this possible?

Thanks in advance for yuor help.

Arlene :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

might be something like

Code: Select all

<?php
$banner = array(
		'toys'=>array('toys1.gif', 'toys2.gif', 'toys3.gif'),
		'tools'=>array('tools1.gif', 'tools2.gif'),
		'food'=>array('food1.gif', 'food2.gif', 'food3.gif', 'food4.gif')
	);

foreach($banner as $key=>$value)
{
	if (strpos($_SERVER['PHP_SELF'], $key) !== FALSE)
	{
		echo $value[rand(0, count($value)-1)];
		return;
	}
}
echo 'nobanner.gif';
?>
where toys,tools,food is something unique in the 'path' to each page-category
arlene
Forum Newbie
Posts: 4
Joined: Fri Mar 14, 2003 3:42 pm

Post by arlene »

Thanks for your help and quick response :)

However, I am such a newbie at this, when I run the code it pretty much works except for the following:

I do not know how to make it display the actual image, all it displays is the text "nobanner.gif" , or the other text if the URL path contains the set word. what is the code to display the actual images in the variable container "toys"?

also, the "nobanner" part displays correctly in my <td>, but if it tries to display something from toys, the HTML output stops processing after it outputs a random link from "toys" so my tables are all messed up.

any insight on this? thanks again for all your help, it is greatly appreciated.

Arlene :D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

the html-element for an image is <img ...>
e.g.

Code: Select all

<?php
$banner = array(
      'toys'=>array('toys1.gif', 'toys2.gif', 'toys3.gif'),
      'tools'=>array('tools1.gif', 'tools2.gif'),
      'food'=>array('food1.gif', 'food2.gif', 'food3.gif', 'food4.gif')
   );

foreach($banner as $key=>$value)
{
   if (strpos($_SERVER['PHP_SELF'], $key) !== FALSE)
   {
      echo '<img src="', $value[rand(0, count($value)-1)], '" />';
      return;
   }
}
echo 'nobanner.gif';
?>
the script was meant to run as an include. If you put it in your main-script return; will end the execution. As an include it will only return to the main script.
arlene
Forum Newbie
Posts: 4
Joined: Fri Mar 14, 2003 3:42 pm

Post by arlene »

Thanks again! added to the include and it worked fine.

I need one more thing that i was not clear on orginally. I need to be able to have 3 specific banners display on a category-specific page and then one slot that rotates like the code you gave me. how would I add an array that ALWAYS displayed 3 ads permanately and then rotated all the rest in the fourth slot with this code?

Once again, thank you for sharing your expertise :)

Arlene
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

three banners that are always the same plus one category specific?
arlene
Forum Newbie
Posts: 4
Joined: Fri Mar 14, 2003 3:42 pm

Post by arlene »

Hello Volka,

Actually, I mean 3 always static CATEGORY specific banners, and 1 slot that rotates the rest of the category specific banners.

like this.

slot 1 = "toy_company_1.gif" permanent
slot 2 = "toy_company_2.gif" permanent
slot 3 = "toy_company_3.gif" permanent

slot 4 = "toy_company_4.gif, toy_company_5.gif, toy_company_6.gif " randomly displayed.

Thanks for your guidance :)

Arlene
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<html>
	<head>
		<!-- example style -->
		<style type="text/css">
			.banner { display: block; margin: 10px; border: 1px solid silver; }
		</style>
	</head>
	<body>

<?php
srand((float)microtime()*10000);
$banner = array(
      'toys'=>array(
      		'static'=>array('toys1.gif', 'toys2.gif', 'toys3.gif'),
      		'random'=>array('toys4.gif', 'toys5.gif', 'toys6.gif')
      	),
      'tools'=>array(
      		'static'=>array('tools1.gif', 'tools2.gif', 'tools3.gif'),
      		'random'=>array('tools4.gif', 'tools5.gif', 'tools6.gif')
      	),
      'food'=>array(
      		'static'=>array('food1.gif', 'food2.gif', 'food3.gif'),
      		'random'=>array('food4.gif', 'food5.gif', 'food6.gif')
      	)
   );

$_SERVER['PHP_SELF'] = 'food';

foreach($banner as $key=>$value)
{
   if (strpos($_SERVER['PHP_SELF'], $key) !== FALSE)
   {
   		foreach($value['static'] as $sbanner)
   			echo '<img class="banner" src="', $sbanner, '" />';
      echo '<img class="banner" src="', $value['random'][rand(0, count($value['random'])-1)], '" />';
      return;
   }
}
echo 'nobanner';
?>
</body></html>
But I should state that this is not a "write-me-the-script" board (usually, but I was in the mood to do so ;) )
Post Reply