简体   繁体   English

将PHP_include用于导航栏不适用于嵌套文件夹

[英]Using PHP_include for navbar does not work with nested folders

I am currently using WAMP/localhost for development of my site. 我目前正在使用WAMP / localhost来开发我的网站。 I did not want to keep having to change my navbar on all of my pages so I moved the navbar into its own file and am using include('navbar.php'). 我不想一直在所有页面上更改导航栏,因此将导航栏移到了自己的文件中,并使用了include('navbar.php')。 It currently works except for when I have nested folders for example, 它目前可以正常工作,例如当我有嵌套文件夹时,

WAMP (folder)
--www (folder)
----index.php
----navbar.php
----lab (folder)
------lab1 (folder)
--------q1.php
--------q2.php

When I view question 1, q1, and try to go back to home, index.php, it works. 当我查看问题1(q1)并尝试回到首页index.php时,它可以工作。 When I try to view q2 from q1 it does not work. 当我尝试从q1查看q2时不起作用。 Instead it displays, /labs/1/labs/1/2.php . 而是显示/labs/1/labs/1/2.php Here is my navbar code that is included on all pages, 这是我所有页面上都包含的导航栏代码,

<div class="col-md-2 hidden-sm hidden-xs" id="sidebar" role="navigation">
  <div class="well sidebar-nav">
    <ul class="nav">
      <li><a href="/index.php">Home</a></li>
      <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Lab 1<b class="caret"></b></a>
                <ul class="dropdown-menu" id="sidebar-dropdown">
                  <li><a href="./labs/1/1.php">Question One</a></li>
                  <li><a href="./labs/1/2.php">Question Two</a></li>
                  <li><a href="./labs/1/4.php">Question Four</a></li>
                  <li><a href="#">sub item</a></li>
                </ul>
      <!--<li><a href="#">Item</a></li>
      <li><a href="#">Item</a></li>
      <li><a href="#">Item</a></li>
      <li><a href="#">Item</a></li>
      <li><a href="#">Item</a></li>-->
    </ul>
  </div><!--/.well -->
</div><!--/span-->

How can I have it work so that when I click on a page within a folder it will load the correct file? 如何使用它,以便在单击文件夹中的页面时可以加载正确的文件?

you need to give the complete address at the href 您需要在href上提供完整的地址

you can have define a variable at top of navbar 您可以在导航栏顶部定义一个变量

$base = 'http://localhost/myproject'; //as per as your site

and the change the href of the tag 并更改标签的href

<li><a href="<?php echo $base; ?>/labs/1/1.php">Question One</a></li>
<li><a href="<?php echo $base; ?>/labs/1/2.php">Question Two</a></li>
<li><a href="<?php echo $base; ?>/labs/1/4.php">Question Four</a></li>

the above will output complete url in href 以上将在href中输出完整的网址

in your case . 就你而言。 indicate the current directory and when you click on Question two it append labs/1/2.php to the current url 指示当前目录,当您单击问题二时,将labs / 1 / 2.php附加到当前URL

You need to set the path correctly whether absolute (starting with a drive letter or \\ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..). 您需要正确设置路径,无论是绝对路径(在Windows中以驱动器号或\\开头,在Unix / Linux系统中以/开头)还是相对于当前目录的路径(以。或..开头)。 For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file. 例如,如果文件名以../开头,则解析器将在父目录中查找以找到请求的文件。 So, it depends where you include it. 因此,这取决于您将其包括在何处。 You need to fix the path so that it points to the target file from the reference file. 您需要修复路径,使其指向参考文件中的目标文件。

It will not find if you include ../ in both current folder and in another folder inside it. 它不会找到您是否同时在当前文件夹和其中的另一个文件夹中都包含../。 To avoid it use, an absolute path or modify relative paths appropriately. 为了避免使用它,请绝对路径或适当地修改相对路径。

You can use a config file (that sits in the root) or you can include the index, depending on your preference. 您可以使用配置文件(位于根目录中),也可以包括索引,具体取决于您的偏好。

In that file you can set.. 您可以在该文件中进行设置。

$rootDir = __DIR__; // or dirname(__FILE__);.. location of the config/index file

$includeDir = $rootDir.DIR_SEP.include.DIR_SEP; // so $rootDir/in

Then you can include the config or index file in whatever nested file you wish but still call... 然后,您可以将config或index文件包含在所需的任何嵌套文件中,但仍可以调用...

include_once($includeDir.'navbar.php');

Everything beyond this, I'm not too sure about. 除此之外,我不太确定。

Or you could use a function (this is a horrible approach but would possibly work) 或者您可以使用一个函数(这是一种可怕的方法,但是可能会起作用)

function getNavBar()
{
    include($includeDir.'navbar.php');
}

Or really crude (in fact I have no idea how well, if at all, this would work).. 或真的很粗糙(实际上,我不知道这有多好,如果可以的话)。

fucntion getNavBar()
{
    ob_start();
    include($includeDir.'navbar.php');
    ob_end_flush();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM