【问题标题】:php if isset _GET chapter or markerphp if isset _GET 章节或标记
【发布时间】:2014-02-21 09:09:25
【问题描述】:

我有一个 index.php,我想在其中生成它应该显示的页面模板。 因此,当您在 index.php 上时,您会看到 home.php 模板。

如果您在 index.php?chapter=chapter-name 上,您会看到 chapter.php 模板。 如果您在 index.php?marker=marker-name 上,您会看到 ma​​rker.php 模板。

我现在有以下内容:

<?php
    if(!isset($_GET["chapter"])){
        $page = "root";
        include_once('view/home.php');
    } else {
        $page = $_GET["chapter"];
        switch($page){
            case "chapter-name":
            include_once('view/chapter.php');
            break;

            case "marker-name":
            include_once('view/marker.php');
            break;
        }
    }
?>

谢谢!

【问题讨论】:

  • 现有代码有问题吗?你有什么问题?
  • 你在这里给出答案或问题吗?

标签: php variables get templating isset


【解决方案1】:
//Array of configuration views
$config_template = array(
    'default' => 'view/home.php' ,
    'chapter-name' => 'view/chapter.php' ,
    'marker-name' => 'view/marker.php' ,
) ;
//Logic to call template
$include = $config_template['default'] ;
if ( isset( $_GET['chapter'] ) && array_key_exists( strtolower( $_GET['chapter'] ) , $config_template ) ) {
    $include = $config_template[$_GET['chapter']] ;
}
else if ( isset( $_GET['marker'] ) && array_key_exists( strtolower( $_GET['marker'] ) , $config_template ) ) {
    $include = $config_template[$_GET['marker']] ;
}
//include template
include_once($include) ;

这样代码就可以长大了……

【讨论】:

    【解决方案2】:

    我想你想要这样的东西

    <?php
        if(isset($_GET["chapter"]) && $_GET["chapter"]=='chapter-name')
        {
            $page = $_GET["chapter"];
            include_once('view/chapter.php');
        }
        else if(isset($_GET["marker"]) && $_GET["marker"]=='marker-name')
        {
            $page = $_GET["marker"];
            include_once('view/marker.php');
        }
        else
        {
            $page = "root";
            include_once('view/home.php');
        }
    ?>
    

    【讨论】:

    • 谢谢,这正是我想要的!
    【解决方案3】:

    也许你想要这样的东西?

    <?php
    if(isset($_GET["chapter"])) {
        $page = $_GET["chapter"];
        include_once('view/chapter.php');
    } else if(isset($_GET["marker"])) {
        $page = $_GET["marker"];
        include_once('view/marker.php');
    } else {
        $page = "root";
        include_once('view/home.php');
    }
    ?>
    

    使用$_GET["chapter"],您将永远不会得到“标记名称”,因为它在$_GET["marker"]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多