views:

373

answers:

2

I use a the following PHP switch for my navigation menu:

<?php include("header.php"); 
    if (! isset($_GET['page']))
    {
        include('./home.php');

    } else {    
        $page = $_GET['page'];  
        switch($page)
        {
            case 'about':
                include('./about.php');
                break;  
            case 'services':
                include('./services.php');
                break;  
            case 'gallery':
                include('./gallery.php');
                break;      
            case 'photos':
                include('./photos.php');
                break;  
            case 'events':
                include('./events.php');
                break;  
            case 'contact':
                include('./contact.php');
                break;
        }
    }
    include("footer.php"); 
    ?>

When I go to my "Photos" section, I am going to have a sublist for other galleries within the photos.

When I am on a page right now, my url looks like this:

index.php?page=photos

I would like to know what PHP code I need to add so when i go to the CARS section I can have my url look like this:

index.php?page=photos&section=cars

?

+1  A: 

Conceptually, wouldn't you just add another nested level of switch or if/then tests?

This could be stuck into your existing switch, but it might be more readable to put it in a function

case: 'photos'
  $section = photo_switch( $_GET['section'] );
  include( $section );
  break;

Or you could just clean the user input and use it:

case 'photos'
  $section = preg_replace( "/\W/", "", $_GET['section'] );
  include( './photos/' . $section . '.php' );
  break
Devin Ceartas
Thank you, I have one more question regarding this switch. I need to pull the second variable into my IF statement, here is a link to the question:http://stackoverflow.com/questions/2256561/php-if-statement-question-how-to-pull-2nd-get-variable
arsoneffect
arsoneffect
what exactly does your code look like now?
Devin Ceartas
A: 

I would take the following approach. It allows you to have arbitrary paths to files and, imho, makes things simpler to expand and to read.

<?php
    include("header.php"); 

    $page = isset($_GET['page']) ? trim(strtolower($_GET['page']))       : "home";

    $allowedPages = array(
        'home'     => './home.php',
        'about'    => './about.php',
        'services' => './services.php',
        'gallery'  => './gallery.php',
        'photos'   => './photos.php',
        'events'   => './events.php',
        'contact'  => './contact.php'
    );

    include( isset($allowedPages[$page]) ? $allowedPages[$page] : $allowedPages["home"] );

    include("footer.php"); 
?>

This same idea can be extended in your photos.php include (or any other file for that matter) to work with the different sections that you may have:

photos.php

<?php
    $section = isset($_GET['section']) ? trim(strtolower($_GET['section'])) : "members";

    $allowedPages = array(
        'members' => './photos/members.php',
        'cars'    => './photos/cars.php'
    );

    include( isset($allowedPages[$section]) ? $allowedPages[$section] : $allowedPages["members"] );
?>
Justin Johnson