0

I normally use a GET variable i n the URL to determined what data to show in my template.

Today I am not passing a variable. I am including a file named inludes/leftmenu.php on every page but depending on the page name I want to show different data.

leftmenu.php looks like so:

$page = $_SERVER["REQUEST_URI"];
$page_name = get_page_name($_SERVER["REQUEST_URI"]);

   if ($page_name == "classes.php")
        {
<h2 class="left_h2">Class Schedules &amp; Info</h2>
<ul class="left_ul">
    <li>Immersion Programs</li>
    <li>Afternoon Programs</li>
    <li>Immersion Schedule</li>
    <li>Afternoon Schedule</li>
</ul>

    }else if($page_name == "about.php")
    {

<h2 class="left_h2">About Us</h2>
<ul class="left_ul">
    <li>About Us</li>
    <li>Photo Scrapbook</li>
</ul>
   }else if ($page_name == "news.php")
    {

<h2 class="left_h2">News &amp; Events</h2>
<ul class="left_ul">
    <li>News</li>
    <li>Events</li>
</ul>

  }
1
  • Sorry,I had two issues in my head while writing this post and I got them mixed up. But you actually both questions got answered. Thanks! Commented Feb 27, 2011 at 23:03

6 Answers 6

2

You could use a PHP array:

$pages = array(
  '/events.php' => 'includes/events_menu.php',
  '/news.php'   => 'includes/news_menu.php'
);

// lookup the appropriate include file
$uri = $_SERVER['REQUEST_URI'];
$include = $pages[$uri];

// produce a default page if the URI wasn't recognised
if (!isset($include)) {
  $include = 'includes/default.php';
}

include($include);
Sign up to request clarification or add additional context in comments.

2 Comments

Can $pages have multiple keys for each value. i.e. $pages = array('/events.php, /home_events.php, away_events.php ' => 'includes/events_menu.php');
no, you'd have to have duplicate keys all mapping individually to the same value.
1

I don't know if it's faster, but there are many ways to do this.

E.g. with an array:

$page_name = get_page_name($_SERVER['REQUEST_URI']);

$includes = array(
    'events.php' => 'includes/events_menu.php',
    'news.php' => 'includes/news_menu.php'
);

if(isset($includes[$page_name])) include($includes[$page_name]);
else die('this page does not exist');

Comments

0

PHP polulates $_GET with variables passed in the URL. Though I do not recommend having a user-passed variable as a function of what your code includes.

an array is a better method, ofcourse, but you may also look into realpath() and make sure it's in DOCUMENT_ROOT /includes

1 Comment

Sorry I asked my question incorrectly. Please see my edits above.
0

You can create array with paths and then use in_array function, which will reduce if statements.

Comments

0

If there's a menu for every single page, you could go with

include('includes/'.basename($page_name, ".php").'_menu.php');

2 Comments

Ouch, I will not be taking this advise !
Robert, if ever you have a moment, could you please let me know why this is such bad advice (for personal knowledge and better understanding)? Thanks. I guess the solution doesn't really apply at all to the edited question, anyway.
0

You can use REQUEST_URI, but you have to keep in mind that it always starts with a / slash. Unless you apply basename() you therefore would have to write:

if ($page_name == "/events.php")

The if statements are a workable approach if you only need a few page names. Otherwise use an switch or an url->filename map (as an array).

If you are using a RewriteRule to map all incoming URLs to a single PHP script, you could also think about using PATH_INFO instead of REQUEST_URI.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.