php如果设置了章节或标记
我有一个index.php,我想在其中生成它应该显示的页面模板。 因此,当您在index.php上时,您会看到home.php模板 如果您在index.php?chapter=chapter name上,您将看到chapter.php模板。 如果您在index.php?marker=marker name上,您将看到marker.php模板 我现在有以下资料:php如果设置了章节或标记,php,variables,get,templating,isset,Php,Variables,Get,Templating,Isset,我有一个index.php,我想在其中生成它应该显示的页面模板。 因此,当您在index.php上时,您会看到home.php模板 如果您在index.php?chapter=chapter name上,您将看到chapter.php模板。 如果您在index.php?marker=marker name上,您将看到marker.php模板 我现在有以下资料: <?php if(!isset($_GET["chapter"])){ $page = "root";
<?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
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');
}
?>
有了$\u GET[chapter],你将永远不会得到“marker name”,因为它在$\u GET[marker]我想你想要这样的东西
<?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');
}
?>
这样代码就可以成长了……现有代码中有问题吗?你的问题是什么?你在这里给出答案还是问题?谢谢,这正是我想要的!
//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) ;