Web Design Forums

Coding Articles & Tutorials

Read about HTML, PHP, Java, and many other popular languages.

[PHP] How to show a different image depending on current page



Site of the Month Voting - Now Open. CAST YOUR VOTE NOW!

Reply
 
LinkBack Thread Tools
Old July 24 '05, 03:40 PM (#1)
bfsog is offline
Coder
 
bfsog's Avatar
 
Join Date: May 2003
Location: UK
Posts: 2,354
bfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to behold
Send a message via MSN to bfsog
[PHP] How to show a different image depending on current page

In this tutorial, I will show you how to change an image, such as your header image, or an image which tells the user what page they are on.

For instance, if your site is of personal nature, your links could be "Home", "Contact", "About" and "Images"

Your site visitor enters through the Home Page, and goes to the Images page, would it be uber to change a certain image? I will show you how.

[What you need to know]

1: The basics of PHP, and a little (X)HTML


[Step 1]

First of all, if you have not done so already, on all the pages that you want to have this effect on, add an <img> tag. Set that img's src to $image

[Step 2]

Then, somewhere in the <head></head> section of all the pages that you want to see this effect on, copy the following code.

PHP Code:
<?php
<?php
// if $_GET['view'] is not set it means the user has just came to your home page, so set $image to 'home.gif'

    
if(isset($_GET['view'])) { $curpage $_GET['view'];}
    else { 
$image "home.gif"; }
    if(
$curpage=="about") { $image "about.gif"; }
    if(
$curpage=="contact") { $image "contact.gif"; }
    if(
$curpage=="images") { $image "images.gif"; }
    if (
$curpage=="home") { $image "home.gif"; }
    
?>
What that code does is checks which page is being viewed, and from that information sets a variable called $image to a different image.

Here is the final step.

[Step 3]

There is just one more thing to do, and that is to alter your navigational links

Lets say your links were something like:
PHP Code:
<a href="index.php" title="Home">Home</a> - <a href="contact.php" title="Contact Me">Contact</a
And so on, you will have to change them to..

PHP Code:
<a href="index.php?view=home" title="Home">Home</a> - <a href="index.php?view=contact" title="Contact Me">Contact</a
And thats that. If you followed my advice correctly, when you go from page to page, the image should reflect the page you are viewing.

[Other Information]

The method I used for this, was to have 1 page, called index.php which shows different information based on the ?view variable, as well as a different image.

If you need any help implementating it, please Private Message through here.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old August 3 '05, 09:26 AM (#2)
John Oliver is offline
New Member!
 
John Oliver's Avatar
 
Join Date: August 2005
Posts: 4
John Oliver is on a distinguished road
Thanks, V Good Tutorial.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old August 3 '05, 04:14 PM (#3)
Shadowfiend is offline
Code beautifully and honorably
 
Shadowfiend's Avatar
 
Join Date: June 2005
Location: Atlanta, GA
Posts: 4,143
Shadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond reputeShadowfiend has a reputation beyond repute
I hadn't noticed this tutorial, but now I see it, I think the if... thens are excessive. You could do it this way, instead:
PHP Code:
    $curPage $_GET['view'];

    switch( 
$curPage )
    {
        case 
'about':
            
$image 'about.gif';
            break;
        case 
'contact':
            
$image 'contact.gif';
            break;
        case 
'images':
            
$image 'images.gif';
            break;
        case 
'home'// optional
        
default:
            
$image 'home.gif';
    } 
Now, granted, this is a bit longer; however, I think it's more understandable due to the usage of a construct meant to handle multiple possibilities easily (switch) instead of one best used to handle a few (if/elseif). In addition, you can handle the default case of using home.gif using the default section of the switch statement, and in the same stroke eliminate the need for checking $curPage == 'home', since either case does the same thing.

Personal opinion, of course.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old August 4 '05, 06:58 AM (#4)
bfsog is offline
Coder
 
bfsog's Avatar
 
Join Date: May 2003
Location: UK
Posts: 2,354
bfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to beholdbfsog is a splendid one to behold
Send a message via MSN to bfsog
Indeed. I wrote this code for this guy I know, so I didnt fine tune it but yes, switch case is better.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

  Web Design Forums » Web Design Help » Articles & Tutorials » Coding Articles & Tutorials

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
User Infomation
Your Avatar

Site Of The Month
Nominate Your Site Now!

Advertisement
WolfCMS.org

Latest Articles
- by RickM
- by bfsog

Advertisement

Partner Links



All times are GMT -4. The time now is 01:54 PM.


WebDesignForums.net is Copyright © 2010 RikeMedia.

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164