Solved

Display Different Graphic On Each Page

Posted on 2014-09-25
6
91 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 109

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 109

Expert Comment

by:Ray Paseur
ID: 40346025
@Chris: just wondering - did you test that code?
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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 109

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help deploying my first MVC.Net app with a SQL Server backend 3 25
How to fix Datetime in MySQL? 4 48
Moving from Mcrypt to OpenSSL 18 45
Help with PHP 13 27
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

856 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