0

我正在创建一个导航栏,如果页面当前处于活动状态,它会自动将类更改为“活动”(使用 php if 语句 [使用当前 URL 进行匹配])

我还希望能够根据用户是否登录来更改标题...现在我通常对此没有问题,但是,因为变量内部有 if 语句,我不知道如何继续。

我的问题是,如果在变量 estabilsihing 中执行 if 语句是不可能的……例如,这是我正在尝试做的,但是它不起作用……有没有办法做到这一点,并且实际上使它起作用。 ..谢谢你提前!

我的代码

---PHP ---

在头上:

<?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; ?>
4

6 回答 6

1

您可以使用三元运算符并使用字符串连接而不是回显:

<?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>

?>

其余的以此类推

于 2012-11-25T09:31:12.170 回答
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 
    }
  }
}
?>
于 2012-11-25T10:08:43.907 回答
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 ...';
于 2012-11-25T09:31:38.170 回答
0

我想你正在寻找这样的东西:

$first_partmainnav = 'tutorials';
echo 'some text ' . ($first_partmainnav == 'tutorials' ? 'active' : '') . ' some more text';
于 2012-11-25T09:33:34.747 回答
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>'
....
?>

相信您可以自己填写其余部分。

当我查看 < li> 行的第一行的语法突出显示时,您会注意到发生了一些奇怪的事情。

于 2012-11-25T09:35:20.243 回答
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.
    */
    ?>
于 2012-11-25T19:03:30.857 回答