douchan0523 2012-05-30 07:21
浏览 28
已采纳

根据某些条件在菜单中提供链接

I have a situation here. In my project the menu section contains some anchor tags, it will work perfectly when we in index page, but moving to other pages i want to give the real links there, so my question is how to check which page is viewing or how to check the the site viewer is not in index page

<li><a href="#home" class="active">Home</a></li>
<li><a href="#about_us">About Us</a></li>

I want to change href conditionally, for example when I'm in index the above href attribute is OK, and when I'm in another page, for example register, then the href attribute change to index.php/site/index#home

Thanks in advance

UPDATE : thank you uttara,I found a solution with the help of her

<?php
$url = $_SERVER["REQUEST_URI"];
$page = pathinfo($url);
$filename = $page['filename'];
$href = ($filename=='root_directory' || $filename=='index'  || $filename=='site')?'':Yii::app()->request->baseUrl;
?>
<a href="<?php echo $href ; ?>#home" class="active">Home</a>
  • 写回答

5条回答 默认 最新

  • dongyi0114 2012-05-30 07:30
    关注
    $url = $_SERVER["REQUEST_URI"];
    $page = pathinfo($url);
    $filename = $page['filename'];
    

    $filename will give you the name of current page being viewed

    and you can check for

    if($filename != 'index')
    {
        echo '<li><a href="index.php/site/index#home" class="active">Home</a></li>
              <li><a href="#about_us">About Us</a></li>';
    }
    else
    {
        echo '<li><a href="#home" class="active">Home</a></li>
                  <li><a href="#about_us">About Us</a></li>';
    }
    

    remember $filename gives you just the filename without extension

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

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用