Solved

Display Different Graphic On Each Page

Posted on 2014-09-25
6
89 Views
Last Modified: 2014-10-09
Hi,

I need to display a different graphic on each page of my site (in the top header).

www.mywebsite.com = graphic01.png
www.mywebsite.com/about = graphic02.png
www.mywebsite.com/contact = graphic03.png
www.mywebsite.com/portfolio = graphic04.png
www.mywebsite.com/services = graphic05.png

Then for all other pages, i just want to display the default graphic (graphic-default.png).

What would be the best way to accomplish this using PHP, thanks in advance for your help.
0
Comment
Question by:oo7ml
  • 3
  • 3
6 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40343866
PHP is self-aware.  You can locate $_SERVER['REQUEST_URI'] and test the value to see if the strings "about", "contact", etc. are present in the variable.  You can use an array to map the names of the pages to the names of the graphics, and choose the right one with an if() statement or a switch() statement.
0
 
LVL 10

Accepted Solution

by:
Chris_Gralike earned 250 total points
ID: 40345958
Code example based on Ray's comment.

<?php
// See http://php.net/manual/en/reserved.variables.server.php for more info on $_SERVER superglobal.

switch($_SERVER['REQUEST_URI']) {
                      case '/about':
                             echo '<img src="./images/graphic02.png" />';
                             break;
                      case '/contact':
                             echo '<img src="./images/graphic03.png" />';
                             break;
                       // etc
                      default:
                             // Mainpage (or case '/' for performance)
                             echo '<img src="./images/graphic00.png" />';
                             break;
}
?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40346025
@Chris: just wondering - did you test that code?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 10

Expert Comment

by:Chris_Gralike
ID: 40346082
As a matter of fact i did.
Why you ask?

EE-contact-.png

(I admit the example is loosely written and requires url rewrite to be configured. It will be more stable using basename and various checks)
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40346144
Just thinking out loud - what if there is a URL parameter request variable?  I think it might be worth isolating the desired substring(s) from the URL.  After I wrote, "switch()" I got to thinking that maybe it wasn't the right application for switch()!
0
 
LVL 10

Expert Comment

by:Chris_Gralike
ID: 40346186
My code doesn't handle that at all and in my opinion it should not.

Its an example that needs refinement (sure). But an example non the less. Copy-pasting any example in production without a second thought and precise consideration is bad practice in any situation. It should be rewritten to answer the requirements set out by the user matching its context, and thus needs 'work' ;-)

On how to overcome the risk you set out without masses of code, is by url rewrite: https://wiki.apache.org/httpd/RewriteQueryString. But this in my opinion is a different question.

But!
of course you are absolutely right the code in the example is a huge risk without additional coding.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about why website design really matters in today's demanding market.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now