duancuan7057 2014-01-12 04:08
浏览 44
已采纳

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

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条回答 默认 最新

  • 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题