0

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?

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Is it possible for me to use this method with something like, <?php include('../../mobile-nav.php') ?> I am not sure how to format this to include the use of $base.
On the pages I am using <?php include('../../mobile-nav.php') ?> which is very file structure specific. If there was a way to replace the ../../ with $base that would be wonderful. But I do not know PHP well enough to know how to do this. Thanks for your help!!
$_SERVER['DOCUMENT_ROOT'] give the root address of the document, so you can use this to make your path absolute. But i feel your code is good there is no problem unless mobile-nav.php is two folder up the heirarchy. Why you want to give absolute path
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.