Jump to content

Echo banner image based on page name?


kade119

Recommended Posts

Hey all,

 

Newb is back, someone help me work up a small script to dynamically call a specific banner image depending on which page is clicked on...

 

contact.php = contact.jpg

work.php = work.jpg etc..

 

 

thanks, just want to see how it would work - trying to learn

 

 

below is a script that just determines if it 's the index and if not uses an alternate image... just thought this might be somewhere to start from...

<?php
function is_homepage()
{
  return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/'));
}

$flash = 'http://www.testsite.com/banner.swf';

if (is_homepage()) {
?>
<object width="550" height="400">
<param name="movie" value="<?php echo $flash ?>">
<embed src="<?php echo $flash ?>" width="550" height="400">
</embed>
</object>
<?php
}
else {
echo '<img src="banner.jpg" alt="Image Banner"/>';
}
?>

Link to comment
Share on other sites

okay i think i understand it...

 

 

 

here is the additional code to finish this minor script

 

print "<img src=\"$banner\" />"

 

so

substr()  this subtracts .php from the file name

strlen()    this counts how many characters

$_SERVER['vars']  and this .. is the location of the file?

Link to comment
Share on other sites

For an explanation of $_SERVER['REQUEST_URI'] take a look at http://www.php.net/manual/en/reserved.variables.server.php

 

So, I am taking the portion of the URL after http://www.mysite.com and manipulating it.

 

$url = $_SERVER['REQUEST_URI'];

This will return /filename.php

 

We will then get use the substr function to get the bit we want for the filename.

 

$banner = substr($_SERVER['REQUEST_URI'], 1);

This says that we want the whole string but to start at the second character (0 being the first).

 

$banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3);

We then want the php extension to be dropped so we find the length of the string and subtract 3 (as php is 3 characters) .

 

$banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3).'jpg';

Then you add the jpg extension.

 

I would suggest googling substr and strlen if you don't know how to use them. Php.net has examples of how to use them as well.

Link to comment
Share on other sites

wow... thank you for the breakdown, that helps a lot

 

so if i want to combine my script that identifies if it's the homepage it uses my flash file otherwise it uses my banner function.  Does this look correct?

 

<?php
function is_homepage()
{
  return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/'));
}

$flash = 'http://www.testsite.com/banner.swf';

if (is_homepage()) {
?>
<object width="550" height="400">
<param name="movie" value="<?php echo $flash ?>">
<embed src="<?php echo $flash ?>" width="550" height="400">
</embed>
</object>
<?php
}
else {
$banner = substr($_SERVER['REQUEST_URI'], 0, (strlen($_SERVER['REQUEST_URI']))-3).'jpg';

print "<img src=\"$banner\" />"
}
?>

Link to comment
Share on other sites

Give it a go and see what happens - you learn more from your mistakes than you will even learn from getting everything right first time.  ;D

 

$banner = substr($_SERVER['REQUEST_URI'], 0, (strlen($_SERVER['REQUEST_URI']))-3).'jpg';

Word of caution, this will return /filename.jpg.

As mentioned in my previous post, use

$banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3).'jpg';

to get rid of the first backslash.

 

Good luck!

Link to comment
Share on other sites

this look better?

 

<?php
function is_homepage()
{
  return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/'));
}
$banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3).'jpg';

$flash= '<script type="text/javascript">
	var params = {
		 wmode: "transparent"
	};
	swfobject.embedSWF("_flash/banner_lecdg.swf", "fpBanner", "950", "300", "9.0.0", "expressInstall.swf");
	</script>';
if (is_homepage()) {
echo $flash;
}

else {

echo "<img src=\"$banner\" />";
}
?>

Link to comment
Share on other sites

need assistance in complete this function, not sure if i'm implementing it correctly

 

this echo, can it be written like that?

echo "<img src='../graphics/$banner' />";

 

<?php
function is_homepage()
{
  return in_array(basename($_SERVER['REQUEST_URI']), array('', 'index.php', 'index.html', '/'));
}
$banner = substr($_SERVER['REQUEST_URI'], 1, (strlen($_SERVER['REQUEST_URI']))-3).'jpg';

$flash= '<script type="text/javascript">
	var params = {
		 wmode: "transparent"
	};
	swfobject.embedSWF("_flash/banner.swf", "fpBanner", "620", "250", "9.0.0", "expressInstall.swf");
	</script>';
if (is_homepage()) {
echo $flash;
}

else {

echo "<img src='../graphics/$banner' />";
}

?>

 

 

and this is on my page include...

<?php print is_homepage($banner) ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.