2014-01-12 04:08 阅读 35


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)
----lab (folder)
------lab1 (folder)

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>
      <!--<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>-->
  </div><!--/.well -->

How can I have it work so that when I click on a page within a folder it will load the correct file?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    dqdpz60048 dqdpz60048 2014-01-12 04:26

    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

    点赞 评论 复制链接分享
  • donglv9813 donglv9813 2014-01-12 04:22

    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.

    点赞 评论 复制链接分享
  • dongye3917 dongye3917 2014-01-12 04:27

    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...


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

    Or really crude (in fact I have no idea how well, if at all, this would work)..

    fucntion getNavBar()
    点赞 评论 复制链接分享