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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘