Header.php how do I...?
Moderator: General Moderators
Header.php how do I...?
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?
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?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
analyze $_SERVER['PHP_SELF'] (there are several other variables in the $_SERVER superglobal as well that can work)
An example:
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;- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Yea... change your echo to
Ps. No bumping threads within 24hours.
Code: Select all
echo '<img src="/images/'.$img.'" alt="'.$img.'">';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.'">';
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.'">';
Jcart wrote:Ps. No bumping threads within 24hours.
25hr gap (exactly, by coincidence)Posted: Sat Nov 05, 2005 5:19 am
Posted: Sun Nov 06, 2005 6:19 am
:/
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!
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.'">';
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.'">';
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.
$_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
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
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.'">';