2012-07-17 46 views
1

我正在學習PHP。我想創建一個菜單,可以打開網站目錄中任何文件夾的頁面。以下是目錄和文件的結構:在php中創建菜單 - 子目錄中的頁面

ROOT DIRECTORY 
dirINCLUDES 
    header.php 
    footer.php 
    navigation.php 

dirPROJECT 
    index.php 
    project1.php 
    project2.php 

index.php 

以下是我的navigation.php:

<ul class="accordion" id="accordion"> 
<li><a href="index.php">home</a></li> 
<li><a href="project/index.php">project</a></li> 
</ul> 

,當我點擊「項目」鏈接現在,它需要我project/index.php,但是當我在那個頁面上並點擊「home」鏈接時,它不會將我帶到主頁。 我已經將所有的php文件都包含在index.php和project/index.php文件的dir INCLUDES中。

任何幫助將不勝感激。 謝謝。

+0

只需添加一個'/'到鏈接的href的開始。一個'/'引用根目錄。 – 2012-07-17 20:33:01

回答

0

嘗試......

<ul class="accordion" id="accordion"> <li><a href="../index.php">home</a></li> <li><a href="index.php">project</a></li> </ul> 
0

嘗試類似的index.php完整路徑,/project/index.php等

<li><a href="/mysite/index.php">home</a></li> 
<li><a href="/mysite/project/index.php">project</a></li> 

編輯鏈接

+0

這需要從本地主機文件,例如:

  • project
  • 將鏈接到本地​​主機/項目/ index.php,但該文件實際上是在localhost/mysite/project/index.php – 2012-07-18 05:41:51

    0
    href="<?echo dirname(dirname(__FILE__)).'directory/file.php';?>" 
    

    href="/directory/file.php" 
    
    +0

    隨着你的代碼,它給像---- c:\ wamp \ www \ mysite/project-select/index.php – 2012-07-18 05:45:53

    +0

    這樣的鏈接,你不應該爲HREF使用dirname,因爲它會給出絕對路徑 – emaillenin 2012-07-19 04:16:52

    0
    function menu($dir){ 
    $files = glob($dir.'/*'); 
    $html = '<ul>'; 
    foreach($files as $file){ 
        if(is_dir($file)){ 
         $html .= menu($file); 
        }else{ 
         $html .= '<li><a href="'. $file .'">'. basename($file,'.php') .'</a></li>'; 
        } 
    } 
    $html .= '</ul>'; 
    return $html; 
    

    }

    echo menu("./*");

    +0

    ,它會列出所有目錄中的所有文件。我想構建網站導航而不是顯示所有頁面。 – 2012-07-18 05:40:46