我正在创建一个导航栏,如果页面当前处于活动状态,则自动将类更改为“活动”(使用php if语句[使用当前URL匹配])
我还希望能够根据用户是否登录来更改标题...现在我通常对此没有任何问题,因为变量中有if语句,我这样做不知道如何继续。
我的问题是,如果在变量建立中发表声明是不可能的......例如,这是我正在尝试做的事情,但它不起作用......有没有办法做到这一点,并且实际制作这行得通... 先谢谢你!
<?php
///// (GETS THE PARTS OF THE CURRENT URL)
error_reporting(0);
$directoryURIbody = $_SERVER['REQUEST_URI'];
$pathbody = parse_url($directoryURIbody, PHP_URL_PATH);
$componentsbody = explode('/', $pathbody);
$first_partsbody = $componentsbody[1];
$second_partsbody = $componentsbody[2];
$third_partsbody = $componentsbody[3];
$fourth_partsbody = $componentsbody[4];
$fifth_partsbody = $componentsbody[5];
?>
<?php
if (!isset($_SESSION['idx'])) { ///////////IF NOT LOGGED IN
if (!isset($_COOKIE['idCookie'])) {//////IF NOT LOGGED IN
$navbar = '
<li class="<?php if ($first_partmainnav=="") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
<li class="<?php if ($first_partmainnav=="tutorials") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>tutorials">Tutorials</a></li>
<li class="<?php if ($first_partmainnav=="resources") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>resources">Resources</a></li>
<li class="<?php if ($first_partmainnav=="library") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>library">Library</a></li>
<li class="<?php if ($first_partmainnav=="our-projects") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>our-projects">Our Projects</a></li>
<li class="<?php if ($first_partmainnav=="community") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>community">Community</a></li>';
}
if (isset($_SESSION['idx'])) { ////////////IF LOGGED IN (WITHOUT COOKIES)
$navbar = '
<li class="<?php if ($first_partmainnav=="") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
<li class="<?php if ($first_partmainnav=="whatever") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>whatever">whatever</a></li>
<li class="<?php if ($first_partmainnav=="justanother") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>justanother">Just Another</a></li>
';
} else if (isset($_COOKIE['idCookie'])) {//IF LOGGED IN (WITH COOKIES)
$navbar = '
<li class="<?php if ($first_partmainnav=="") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
<li class="<?php if ($first_partmainnav=="whatever") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>whatever">whatever</a></li>
<li class="<?php if ($first_partmainnav=="justanother") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>justanother">Just Another</a></li>
';
}
?>
<?php echo $navbar; ?>
答案 0 :(得分:1)
您可以使用三元运算符并使用字符串连接而不是echoing:
<?php
if (!isset($_SESSION['idx'])) { ///////////IF NOT LOGGED IN
if (!isset($_COOKIE['idCookie'])) {//////IF NOT LOGGED IN
$navbar = '
<li class="'. ($first_partmainnav=="" ? "active" : "noactive") .'"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
?>
其余的比喻
答案 1 :(得分:1)
li标签中的变量没有任何问题,问题在于将HTML和PHP代码和标签混合在单引号中。除非你纠正,否则什么都不会奏效。以下是使用您自己的代码正确执行此操作的方法:
<?php
if ( !isset( $_SESSION[ 'idx' ] ) ) { ///////////IF NOT LOGGED IN
if ( !isset( $_COOKIE[ 'idCookie' ] ) ) { //////IF NOT LOGGED IN
?>
<li class="<?php if ( $first_partmainnav == "" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
<li class="<?php if ( $first_partmainnav == "tutorials" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>tutorials">Tutorials</a></li>
<li class="<?php if ( $first_partmainnav == "resources" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>resources">Resources</a></li>
<li class="<?php if ( $first_partmainnav == "library" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>library">Library</a></li>
<li class="<?php if ( $first_partmainnav == "our-projects" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>our-projects">Our Projects</a></li>
<li class="<?php if ( $first_partmainnav == "community" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>community">Community</a></li>
<?php }
if ( isset( $_SESSION[ 'idx' ] ) ) { ////////////IF LOGGED IN (WITHOUT COOKIES)
?>
<li class="<?php if ( $first_partmainnav == "" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
<li class="<?php if ( $first_partmainnav == "whatever" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>whatever">whatever</a></li>
<li class="<?php if ( $first_partmainnav == "justanother" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>justanother">Just Another</a></li>
<?php } else {
if ( isset( $_COOKIE[ 'idCookie' ] ) ) { //IF LOGGED IN (WITH COOKIES)
?>
<li class="<?php if ( $first_partmainnav == "" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
<li class="<?php if ( $first_partmainnav == "whatever" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>whatever">whatever</a></li>
<li class="<?php if ( $first_partmainnav == "justanother" ) { echo "active"; } else { echo "noactive"; }?>"><a href="<?php echo $dyn_wwwFULL; ?>justanother">Just Another</a></li>
<?php
}
}
}
?>
答案 2 :(得分:0)
虽然我个人会改变这样做的整个方法(有一个与你的列表相对应的数组并相应地回显),你的方法可以通过将变量与代码分开来完成,例如:
$array['tutorials'] = ($first_partmainnav == "tutorials") ? 'active' : 'noactive';
$array['resources'] = ($first_partmainnav == "resources") ? 'active' : 'noactive';
// etc...
然后将它与您的输出连接。
通常你应该使用字符串连接:
错:
echo '<a href="<?php echo $dyn_wwwFULL; ?>tutorials ...';
右:
echo '<a href="'.$dyn_wwwFULL.'tutorials ...';
答案 3 :(得分:0)
我认为你正在寻找这样的东西:
$first_partmainnav = 'tutorials';
echo 'some text ' . ($first_partmainnav == 'tutorials' ? 'active' : '') . ' some more text';
答案 4 :(得分:0)
有一段时间我用PHP编写代码,但它看起来像你
以下代码如何工作?
<?php
if (!isset($_SESSION['idx'])) { ///////////IF NOT LOGGED IN
if (!isset($_COOKIE['idCookie'])) {//////IF NOT LOGGED IN
$navbar = '
<li class="' if($first_partmainnav=="") {echo "active"; } else {echo "noactive";} '"><a href="' echo $dyn_wwwFULL; '">Home</a></li>'
....
?>
相信你可以自己填写其余部分。
当我查看&lt;第一行的语法高亮显示时李&GT;你注意到有些奇怪的事情发生了。
答案 5 :(得分:0)
真正的问题是您不能在引用的PHP代码中包含PHP标记。让我们在问题中检查第一个li块的最后一行:
<?php
//NOTE: Had to add a space to separate php tags in comments for proper code highlighting, like this: < ? php --- ? >
$navbar = '
<li class="<?php if ($first_partmainnav=="community") {echo "active"; } else {echo "noactive";}?>"><a href="<?php echo $dyn_wwwFULL; ?>community">Community</a></li>'; ?>
<?php
/* Although the whole $navbar value is quoted (Single quotes), there is php code surrounded by php tags in 2 places:
< ? php if ($first_partmainnav=="community") {echo "active"; } else {echo "noactive";} ? > Here
< ? php echo $dyn_wwwFULL; ? > And here
Same happens with the rest of the lines, so it is impossible for this code to work.
*/
?>
所选答案也是错误的。我们来看看:
<?php
$navbar = '<li class="'. ($first_partmainnav=="" ? "active" : "noactive") .'"><a href="<?php echo $dyn_wwwFULL; ?>">Home</a></li>
'; // Add the single quote an semicolon missing.
/* Same problem: $navbar value is quoted but there is php code surrounded by php tags:
<a href="< ? php echo $dyn_wwwFULL; ? > Here.
This code can't work either.
*/
?>