2014-02-21 27 views
1

我有一個index.php並在其中我想要生成它應該顯示的頁面模板。 所以,當你在index.php你看到home.php模板。php ifset _GET章節或標記

如果你在的index.php?章=章名看到chapter.php模板。 如果你在index.php?marker = marker-name你會看到marker.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; 
     } 
    } 
?> 

謝謝!

+5

現有代碼中的任何問題?你的問題是什麼? – Roopendra

+0

你在這裏給出答案或問題嗎? –

回答

0
//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) ; 

這樣的代碼已準備就緒長大...

0

也許你想要這樣的事情?

<?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"]

0

我想你想是這樣的

<?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'); 
    } 
?> 
+0

感謝這正是我想要的! –