简体   繁体   中英

Using PHP_include for navbar does not work with nested folders

I am currently using WAMP/localhost for development of my site. 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'). 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. When I try to view q2 from q1 it does not work. Instead it displays, /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

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

<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

in your case . indicate the current directory and when you click on Question two it append labs/1/2.php to the current 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 ..). 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...

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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