我有一个if语句的问题,我正在尝试使用php include激活我正在使用的页面,这是我的包含代码;
<?php
echo '
<div class="container">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="/womsy/home.php">Womsy</a>
</div>
<ul class="nav navbar-nav">
<li <?php if ($page = "home") {class=active;} else {echo "Failed to load active class."} ?><a href="?page=home">Home</a></li>
<li><a href="?page=blog">Blog</a></li>
<li><a href="?page=projects">Projects</a></li>
<li><a href="?page=contact">Contact</a></li>
<li><a href="?page=about">About</a></li>
</ul>
</div>
</nav>';
?>
这就是我的方式:
<?php
include ('php/includes/menu.php');
?>
所以我试图激活“主页”页面,有人可以帮我这个吗?
格尔茨
答案 0 :(得分:0)
试试这个:
<?php
$page = "";
if(isset($_GET["page"]) && $_GET["page"] != "") {
$page = $_GET["page"];
}
?>
<div class="container">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="/womsy/home.php">Womsy</a>
</div>
<ul class="nav navbar-nav">
<li <?php if ($page == "home") { echo "class='active'";} ?>><a href="?page=home">Home</a></li>
<li <?php if ($page == "blog") { echo "class='active'";} ?>><a href="?page=blog">Blog</a></li>
<li <?php if ($page == "projects") { echo "class='active'";} ?>><a href="?page=projects">Projects</a></li>
<li <?php if ($page == "contact") { echo "class='active'";} ?>><a href="?page=contact">Contact</a></li>
<li <?php if ($page == "about") { echo "class='active'";} ?>><a href="?page=about">About</a></li>
</ul>
</div>
</nav>
答案 1 :(得分:0)
您需要修复if块
if ($page = "home")
到
if ($page == "home")
答案 2 :(得分:0)
我认为你有错误
<?php if ($page = "home") {class=active;} else {echo "Failed to load active class."} ?>
应该是
<?php if ($page == "home") {class=active;} else {echo "Failed to load active class."} ?>