Header.php how do I...?

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

MartinaL
Forum Newbie
Posts: 10
Joined: Fri Nov 04, 2005 9:31 pm

Header.php how do I...?

Post by MartinaL »

I have a header.php included on each of my pages, but i want it to display a different image depending on the page.

How do i do this?

For example i have the following pages - index.php, about.php, music.php and merch.php and for each of these the header will include a banner - banner1.jpg, banner2.jpg, banner3.jpg, banner4.jpg.

How do i write this in the header file?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

analyze $_SERVER['PHP_SELF'] (there are several other variables in the $_SERVER superglobal as well that can work)

An example:

Code: Select all

switch(basename($_SERVER['PHP_SELF'])) {
case 'index2.php':
  $img = 'banner2.jpg';
  break;
case 'somethingElse.php':
  $img = 'banner1.jpg';
  break;
default:
  $img = 'banner.jpg';
  break;
}

echo $img;
MartinaL
Forum Newbie
Posts: 10
Joined: Fri Nov 04, 2005 9:31 pm

Post by MartinaL »

This half works for me, but instead of displaying the image is is displaying the img name - so instead of seeing an image you see; images/dhopecbanner1.jpg
MartinaL
Forum Newbie
Posts: 10
Joined: Fri Nov 04, 2005 9:31 pm

Post by MartinaL »

Does anyone have any ideas why this might be happening?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Yea... change your echo to

Code: Select all

echo '<img src="/images/'.$img.'" alt="'.$img.'">';
Ps. No bumping threads within 24hours.
MartinaL
Forum Newbie
Posts: 10
Joined: Fri Nov 04, 2005 9:31 pm

Post by MartinaL »

I got this working for for adding the header to all of the pages, but now I have a problem because I want to add a specfic header to all of the files in a subdirectory??

This is the code that I am using to add the header to my other files which are in the same directory as the header file, but how do I do it when so that all of the files in a folder called gigs have say img header8.jpg which is in a folder called images?

switch(basename($_SERVER['PHP_SELF'])) {
case 'index.php':
$img = 'dhopecbanner1.jpg';
break;
case 'gigs.php':
$img = 'dhopecbanner4.jpg';
break;
case 'contact.php':
$img = 'banner8.jpg';
break;
case 'music.php':
$img = 'banner3.jpg';
break;
case 'merch.php':
$img = 'banner5.jpg';
break;
case 'band.php':
$img = 'banner2.jpg';
break;
case 'calander.php':
$img = 'dhopecbanner4.jpg';
break;
case 'success.php':
$img = 'banner5.jpg';
break;
case 'cancel.php':
$img = 'banner5.jpg';
break;
default:
$img = 'banner8.jpg';
break;
}
echo '<img src="images/'.$img.'" alt="'.$img.'">';
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Jcart wrote:Ps. No bumping threads within 24hours.
Posted: Sat Nov 05, 2005 5:19 am

Posted: Sun Nov 06, 2005 6:19 am
25hr gap (exactly, by coincidence)

:/

MartinaL -

I don't understand what you mean, can you explain please - do you mean:

You have a folder called gigs, with a sub directory images. Within the gigs folder are all of your pages and in the images folder, all the images. Images are selected dependant upon which page the user is viewing.

Or do you mean you want the same image on all pages?

Either way, they are both quite 'easy', so experiment!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Jenk wrote:
Jcart wrote:Ps. No bumping threads within 24hours.
Posted: Sat Nov 05, 2005 5:19 am

Posted: Sun Nov 06, 2005 6:19 am
25hr gap (exactly, by coincidence)

:/
We all make mistakes. I failed to notice the date was different... thanks for pointing mine out ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

No problem :P
MartinaL
Forum Newbie
Posts: 10
Joined: Fri Nov 04, 2005 9:31 pm

Post by MartinaL »

ok i have the mail directory which holds all of the php files gigs, contact etc and then there are 2 subdirectories one called gigs and one called images.

With the code i am already using to display a different image at the top of each of these pages i was hoping to use the same header file to show an image at the top of all of the files in the sub-directory called gigs.

My two problems are this;

1. with this bit i'm not sure how to write the case statement to say "all of the files in the gigs directory"

case 'cancel.php':
$img = 'banner5.jpg';
break;

2. because the case would be for a sub-directory does this mean that to show images i would have to have ../images/banner5.jpg ?

i am already setting all of the $img to so that I only have to write the image name not /images/banner5.jpg with this line - echo '<img src="images/'.$img.'" alt="'.$img.'">';
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

<?php

if ((strpos($_SERVER['SCRIPT_FILENAME'], '/gigs/')) !== false) {
    //file is in the gigs directory
} else {
    //file is not in the gigs directory
}
?>
:)

btw - I think you have the term sub-directory confused, a sub-directory is a child directory. For example, path: /a/b/ - b is a sub-directory of a. a is the parent directory of b.

:)
MartinaL
Forum Newbie
Posts: 10
Joined: Fri Nov 04, 2005 9:31 pm

Post by MartinaL »

Sorry if this sound stupid but what does that bit of code do??

Sorry, amd new to php and still learning 8O
MartinaL
Forum Newbie
Posts: 10
Joined: Fri Nov 04, 2005 9:31 pm

Post by MartinaL »

ok this is what my directory structure looks like


---(contains header.php, contact.php etc)
|-------------------------------------------------images (folder),gigs(folder)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

$_SERVER['SCRIPT_FILENAME'] contains the full (absolute) path to the current script file.

strpos() is a function used to find the position of a string within a string, if no match, it returns false, this makes it useful for checking if string A is within string B at all :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

The other option to avoid confusion with directories etc. is to just echo the full path of the images folder, instead of a relative path:

Code: Select all

echo '<img src="http://www.yourwebsite.com/images/'.$img.'" alt="'.$img.'">';
Post Reply