英文:
Dynamic menu active not working? (if condition not working )
问题
以下是您要翻译的内容:
// 获取页面名称开始
<?php include('db/db.php'); $page = $_SERVER['REQUEST_URI']; $pagename = str_replace('/project/',' ',$page); ?>
// 结束
<div class="collapse navbar-collapse" id="navbar">
<ul class="navbar-nav ml-auto">
<li><a <?php if($pagename=="index.php" ){ ?> class="active" <?php } ?> href="index.php">首页</a></li>
<li><a <?php if($pagename=='login.php' ){ ?> class="active" <?php } ?> href="login.php">登录</a></li>
<li><a href="faq.php" <?php if($pagename=='faq' ){ ?>class="active"<?php } ?>>常见问题</a></li>
<li><a <?php if($pagename=='contact.php' ){ ?>class="active"<?php } ?> href="contact.php">联系我们</a></li>
<!-- 下面是静态菜单 -->
<li class="active"><a href="contact.php">联系我们</a></li>
</ul>
</div>
英文:
// start get page name
<?php include('db/db.php'); $page = $_SERVER['REQUEST_URI']; $pagename = str_replace('/project/'," ",$page); ?>
//end
<div class="collapse navbar-collapse" id="navbar">
<ul class="navbar-nav ml-auto">
<li><a <?php if($pagename=="index.php" ){ ?> class="active" <?php } ?> href="index.php"> Home</a></li>
<li><a <?php if($pagename=='login.php' ){ ?> class="active" <?php } ?> href="login.php">Login</a></li>
<li><a href="faq.php" <?php if($pagename=='faq' ){ ?>class="active"<?php } ?> >Faq</a></li>
<li><a <?php if($pagename=='contact.php' ){ ?>class="active"<?php } ?> href="contact.php">Contact</a></li>
!-- static menu here below -->
<li class="active"><a href="contact.php">Contact</a></li>
</ul>
</div>
答案1
得分: 0
你正在用空格替换 /project/,这将输出带有前导空格的文件名,例如 " index.php"。
$pagename = str_replace('/project/', ' ', $page);
你需要不带任何空格地进行替换,如下所示:
$pagename = str_replace('/project/', '', $page);
这将输出不带空格的文件名。
英文:
You are replacing /project/ with a space and this will output filename like " index.php" with a preceding space.
$pagename = str_replace('/project/'," ",$page);
you need to replace it without any space like
$pagename = str_replace('/project/',"",$page);
this will output filename without space.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论