简体   繁体   English

PHP Switch Nav系统-如何设置它以使用2个GET变量

[英]PHP Switch Nav system - how can i set it up to use 2 GET variables

I use a the following PHP switch for my navigation menu: 我将以下PHP开关用于导航菜单:

<?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: 我想知道我需要添加什么PHP代码,因此当我转到CARS部分时,我的网址应如下所示:

index.php?page=photos&section=cars

?

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 include(或与此相关的任何其他文件)中扩展相同的想法,以与您可能拥有的不同部分配合使用:

photos.php 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"] );
?>

Conceptually, wouldn't you just add another nested level of switch or if/then tests? 从概念上讲,您是否只是添加另一个嵌套级别的switch或是否/然后进行测试?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM