标题中的网站导航


Website Navigation within the Header

我在网站上正确设置导航时遇到问题。我把导航放在我的标题中,我想让所有的链接都能工作,无论网页在文件结构的哪个层。

这是我的文件结构:

 Website (directory)
      index.php
      resources (directory)
      includes (directory)
           html_codes.php
      Game (directory)
           game_info.php
           resources 
           Characters (directory)  
                characters_info.php
                     Players (Directory)
                          players_info.php

在html_codes.php中有一个函数,它为网站构建标题。我使用这个函数在所有网页的顶部创建标题。然而,在网站上导航的超链接在除了顶层目录之外的任何层都不起作用,因为它们的相对位置已经改变。

有没有一种方法可以让链接在中工作

 ./index.php
 ./game/game_info.php
 ./game/Characters/characters_info.php
 ./game/Characters/Players/players_info.php

来自同一个create_header()函数?

我的create_header()函数:

function create_Header(){
echo '
    <div id="top_header">mywebsite.com </div>
    <nav id="top_menu">
        <ul>
            <li><a href="index.php" target="_self">Home</a></li>
            <li><a href="./game/game_info.php" target="_self">Game</a></li>
            <li><a href="./game/Characters/characters_info.php" target="_self">Characters</a></li>  
            <li><a href="./game/Characters/Players/players_info.php" target="_self">Players</a></li>
        <ul>
    </nav>
';
}

*********************************编辑*******************************

好吧,我使用了你的建议,它解决了一些问题,也创造了其他问题。

首先,将路径从相对转为绝对有效,但需要将路径名称更改为

 /Website/index.php
 /Website/game/game_info.php
 /Website/game/Characters/characters_info.php
 /Website/game/Characters/Players/players_info.php

这很好,但我不明白为什么。我认为这是因为Website目录是C:''examplep''htdocs''Website的子目录。

另一个问题是我的include函数不能使用这个绝对路径。之前game_info.php内部include的相对路径是

include("./../includes/html_codes.php");

它过去和现在都有效。

两种我都试过了

include("/includes/html_codes.php");
include("/testphp/includes/html_codes.php");

他们没有工作。

两件事1.我建议不要在源文件和标题中使用大写的文件夹名称。否则,某些浏览器可能会要求用户使用大写字母。这并不是真正的标准。2.我建议你使用绝对路径(已删除)

function create_Header(){
echo '
    <div id="top_header">mywebsite.com </div>
    <nav id="top_menu">
        <ul>
            <li><a href="/index.php" target="_self">Home</a></li>
            <li><a href="/game/game_info.php" target="_self">Game</a></li>
            <li><a href="/game/characters/characters_info.php" target="_self">Characters</a></li>   
            <li><a href="/game/characters/players/players_info.php" target="_self">Players</a></li>
        <ul>
    </nav>
';
}

在路径前面放一个正斜杠/,使它们成为绝对路径。

<a href="/index.php">Go home</a>

这将确保你总是从同一个地方(根)开始,并且你的链接独立于层次结构。

如果你的网站是实时的,你可以使用一个像这样的绝对URL:

<a href="http://www.website.com/index.php">Go home</a>

./是相对于当前目录的。有两种方法可以解决。第一种:继续使用相对路径,但也让它们向上移动。因此,如果您在./game/Characters/characters_info.php上,则目录为./game/Characters/,指向主页的路径为../../index.php

因此,您不仅需要知道当前页面的深度,还需要达到当前页面和目标页面之间共享的级别。毕竟,两者都可能是两个级别的深度,但仍然在不同的目录中。

我认为更好(至少更容易)的方法是使用绝对路径。要做到这一点,您只需从/开始,就可以获得域名的绝对路径。

或者,您可以配置"安装路径"或"基本目录"来启动每个路径。它可以默认为"/",但如果站点完全位于子目录中,则可以将其设置为另一个值。

所以标题看起来像:

<div id="top_header">mywebsite.com </div>
<nav id="top_menu">
    <ul>
        <li><a href="/index.php" target="_self">Home</a></li>
        <li><a href="/game/game_info.php" target="_self">Game</a></li>
        <li><a href="/game/Characters/characters_info.php" target="_self">Characters</a></li>   
        <li><a href="/game/Characters/Players/players_info.php" target="_self">Players</a></li>
    <ul>
</nav>

或者,使用一个基本路径变量,您可以将站点移动到另一个目录,只需更改变量,一切都会正常工作。非常方便测试和开发。此变量的正常值可以仅为''

<div id="top_header">mywebsite.com </div>
<nav id="top_menu">
    <ul>
        <li><a href="<?=$basepath?>/index.php" target="_self">Home</a></li>
        <li><a href="<?=$basepath?>/game/game_info.php" target="_self">Game</a></li>
        <li><a href="<?=$basepath?>/game/Characters/characters_info.php" target="_self">Characters</a></li> 
        <li><a href="<?=$basepath?>/game/Characters/Players/players_info.php" target="_self">Players</a></li>
    <ul>
</nav>