Help With If/Else Statement

I am a PHP newbie and I apologize in advance if this is an extremely easy issue to solve.

I have a PHP page that uses an include to pull a new block of html into the main document. I am wanting to use PHP to determine which page or URL that is being displayed and dynamically choose the include based on that URL.

So if the viewer was to click on a page entitled “Products” then it would display an include that corresponds to the URL. Lets say the Products page is “http://www.domain.com/products.html”. I would want the PHP include to look for a file entitled “products.php” respectively.

Would this need to be written with an If/Else statement? And if so can someone help me write it?

Thanks in advance to anyone who can help!
Todd

a switch and if/else are comparable, except in this case I think case is going to the better option, purely because its easier to read.

An equivalent if/else:


if ($area == 'members') 
{
    include 'sidebar-members.php';
} elseif ($area == 'admin') { 
    include 'sidebar-admin.php';
} else {
    include 'sidebar-default.php';
}

create your plain files: sidebar-members.php, sidebar-admin.php (or whatever).

There are two methods you can use. The first is to switch based on the current filename.

// get current page filename
$page_parts = explode("/", $_SERVER['PHP_SELF']);
define('PAGE_FILENAME', $page_parts[count($page_parts)-1]); 

switch (PAGE_FILENAME)
{
    case 'admin.php':
        include 'sidebar-admin.php';
    break;

   case 'members.php':
       include 'sidebar-members.php';
   break;

   // you can match 2 cases like this
   case 'index.php':
   case 'articles.php':
       include 'sidebar-public.php';
   break;
}  
   // add as many cases as you need. 

The second method which is probably a bit more flexible is to define the page type on your various pages like admin.php, members.php.


// members.php
define('AREA', 'members'); 

include 'sidebar.php';

and now for a global sidebar.php

// sidebar.php
switch (AREA)
{
    case 'members':
        include 'sidebar-members.php';
    break;

    // default to show if no matches are found 
    default:
        include 'sidebar-default.php';
    break;
}

OPTIONAL - Here is how you can use method 2 as a function instead with a variation on the way you define the area.


// members.php

include 'functions.php';
getSidebar('members');

Create a functions.php file.

// functions.php
function getSidebar($area)
{
    switch ($area)
    {
        case 'members':
            include 'sidebar-members.php';
        break;

        // default to show if no matches are found 
        default:
            include 'sidebar-default.php';
        break;
    }
}

Can you show me how you would write this?

to get a sidebar with variable content, probably the most simple approach is to run an if/else or switch statement based on the current filename as suggested above.

To keep your files clean, you could have the switch or if/else embedded in a function, then have separate includes for your different sidebars.

eg. sidebar-members.php, sidebar-admin.php.

Wouldn’t either of these functions be wrapped or contained within an if/else statement? If so, can you show me how that would look? Thank you for the input!

Its essentially a one page template or theme. The file is named index.php and it currently uses an include for one PHP page, for example sidebar.php. My thought is that I would like to load different content into that sidebar area depending on which page it has loaded. Does this make more sense?

Why not just make the location “http://www.domain.com/products.php”? Then no need to faff about with includes. The logic seems rather redundant (or lacking in logic actually).

However, if for some reason you realy need to do it the way you describe it, then I think the best idea is to obtain the page URI via $_SERVER['REQUEST_URI'] and then parse it with parse_url to get the filename. Then you simply include it by appending ‘.php’.

That is what I am looking for, I think, but again I am a newbie and do not know how to write that statement. Could you reply with a snippet for me to try out?