2015-07-10 67 views
2

我有一個關於如何動態更改要在網頁中顯示的內容的問題。 我有幾個固定的網站部分 - 標題,導航,頁腳,飛濺和邊欄。使用PHP的動態導航包括

我只希望我的網站的中間部分根據菜單鏈接改變用戶點擊的內容。

下面是我的index.php代碼

<?php 
include "/templates/header.php"; 
include "templates/menu.php"; 
include "/templates/splash.php"; 
$action = "index"; 
$disallowed_paths = array('header','menu','splash','bottom_page', 'footer'); 
if (!empty($_GET['action'])) 
{ 
    $tmp_action = basename($_GET['action']); 
     if (!in_array($tmp_action, $disallowed_paths) && file_exists("/content/$tmp_action.php")) 
     $action = $tmp_action; 
} 
include "/content/$action.php"; 
include "/templates/bottom_page.php"; 
include "/templates/footer.php"; 
?> 

menu.php包含首頁鏈接,關於產品服務登錄鏈接 我只是想將main_index.php改爲include以上基於wh在用戶點擊。

請指教,如果這種方法是好的。 或者我應該創建類似的文件作爲的index.php多次使用包括對每個文件按鏈接菜單上點擊

+0

看看智者或simular模板系統,以更好地理解 – donald123

回答

0

你的答案是

GET方法

你可以使用get方法

<ul> 
     <li><a href="?page=example_1">example 1</a></li> 
     <li><a href="?page=example_2">example 2</a></li> 
     <li><a href="?page=example_3">example 3</a></li> 
     <li><a href="?page=example_4">example 4</a></li> 
</ul> 

    After User Clicks on the link 

    <?php 
     if(isset($_GET['page']) && $_GET['page']!=""){ 
      $page = ""; 
      switch ($_GET['page']) { 
       case 'example_1': 
        $page = "Page_1.php"; 
        break; 

       case 'example_2': 
        $page = "Page_2.php"; 
        break; 

       case 'example_3': 
        $page = "Page_3.php"; 
        break; 

       case 'example_4': 
        $page = "Page_4.php"; 
        break; 

       default: 
        $page = "any_default_page.php"; 
        break; 
      } 

      include($page); 
     } 
    ?> 

還有其他方法。但這是最容易和最有效的

0

我認爲更好的方法將白名單,而不是黑名單。

$existing_pages = array('home', 'contact', 'terms-of-service'); 

if(isset($_GET['action'] && in_array($_GET['action'], $existing_pages) 
{ 
    // load action here 
} else { 
    //show 404 error 
} 

請注意,您的總體做法是不理想的,你可以看看像融入現代框架,如LaravelSymphony具有模板系統,幫助了很多東西。

但對於學習的目的是,如果要編程香草或使用細:)