我有nav.php
,里面有2个变量。我希望通过回显变量在nav.php
中包含index.php
。 nav.php
代码如下:
<?php
$TopLeftNav = '<li if ($_SERVER['PHP_SELF'] == "/dashboard.php") echo "class = 'active'" ><a href="dashboard.php">Dashboard</a></li>
<li if ($_SERVER['PHP_SELF'] == "/website.php") echo "class = 'active'" ><a href="website.php">Add Website</a></li>
<li php if ($_SERVER['PHP_SELF'] == "/report.php") echo "class = 'active'" ><a href="report.php">Reports</a></li>';
$TopRightNav = '<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>';
?>
正如您所看到的,变量$TopLeftNav
不会起作用,因为if
条件有效。条件是突出显示当前活动页面。
我知道我可以通过创建单独的文件(例如nav1.php
,nav2.php
并将导航html分开来使其工作,但出于学习目的,我该如何解决此问题?提前谢谢。
答案 0 :(得分:2)
这应该适合你:
我在这里使用这样的三元运算符:
(conditional expression) ? (ouput if true) : (output if false);
<?php
$TopLeftNav = '<li ' . ($_SERVER['PHP_SELF'] == "/dashboard.php" ? ' class = \'active\' ' : '') . ' ><a href="dashboard.php">Dashboard</a></li>
<li ' . ($_SERVER['PHP_SELF'] == "/website.php" ? ' class = \'active\' ' : '') . ' " ><a href="website.php">Add Website</a></li>
<li ' . ($_SERVER['PHP_SELF'] == "/report.php" ? ' class = \'active\' ' : '') . ' " ><a href="report.php">Reports</a></li>';
$TopRightNav = '<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>';
?>
答案 1 :(得分:2)
<?php
// set the variables we will use to an empty string
$web_class = '';
$dash_class = '';
$report_class = '';
// check the PHP_SELF and set a variable
if ($_SERVER['PHP_SELF'] == "/website.php") {
$web_class = 'active';
} elseif ($_SERVER['PHP_SELF'] == "/dashboard.php") {
$dash_class = 'active';
} elseif ($_SERVER['PHP_SELF'] == "/report.php") {
$report_class = 'active';
}
// generate the output
$TopLeftNav = '<li class="'.$dash_class.'"><a href="dashboard.php">Dashboard</a></li>';
$TopLeftNav .= '<li class="'.$web_class.'"><a href="website.php">Add Website</a></li>';
$TopLeftNav .= '<li class="'.$report_class.'"><a href="report.php">Reports</a></li>';
$TopRightNav = '<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>';
?>
虽然我更倾向于做这样的事情:(转换声明)http://php.net/manual/en/control-structures.switch.php
<?php
// set the variables we will use to an empty string
$web_class = '';
$dash_class = '';
$report_class = '';
switch through the PHP_SELF and set the variables accordingly
switch ($_SERVER['PHP_SELF']){
case '/dashboard.php':
$dash_class = 'active';
break;
case '/report.php':
$report_class = 'active';
break;
case '/website.php':
$web_class = 'active';
break;
}
// generate the output
$TopLeftNav = '<li class="'.$dash_class.'"><a href="dashboard.php">Dashboard</a></li>';
$TopLeftNav .= '<li class="'.$web_class.'"><a href="website.php">Add Website</a></li>';
$TopLeftNav .= '<li class="'.$report_class.'"><a href="report.php">Reports</a></li>';
$TopRightNav = '<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>';
?>
switch语句非常适合这种事情,因为当您在几年后回到它时,生成的代码更具可读性。
答案 2 :(得分:2)
好的,因为其他答案看起来更像是黑客而不是有用的东西,请考虑这种方法:
<?php
// three classes for each for easier adding them to the HTML strings
// each of them will be assigned a value of "class = 'active'" in case of matching urls
// or just an empty string otherwise
$dashboardClass = $_SERVER['PHP_SELF'] == "/dashboard.php" ? "class = 'active'" : "";
$reportClass = $_SERVER['PHP_SELF'] == "/report.php" ? "class = 'active'" : "";
$websiteClass = $_SERVER['PHP_SELF'] == "/website.php" ? "class = 'active'" : "";
$TopLeftNav = "
<li $dashboardClass><a href='dashboard.php'>Dashboard</a></li>
<li $websiteClass><a href='website.php'>Add Website</a></li>
<li $reportClass><a href='report.php'>Reports</a></li>";
$TopRightNav = '<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>';
?>
因此,我们的想法是存储要为三个类中的每一个回显的值(因此我们有三个新变量)。我也从单引号切换到双引号,以便直接插入PHP字符串,而不必连接任何内容,因此例如<li $dashboardClass>
将成为<li class='active'>
,以防PHP_SELF与dashboard.php匹配,或者只是<li>
。
三元运营商:
这:$dashboardClass = $_SERVER['PHP_SELF'] == "/dashboard.php" ? "class = 'active'" : "";
与此相同:
if ($_SERVER['PHP_SELF'] == "/dashboard.php") {
$dashboardClass = "class = 'active'";
}
else {
$dashboardClass = '';
}