简体   繁体   English

这个三元运算符怎么了?

[英]What's wrong with this ternary operator?

I use a paging system like this: 我使用这样的分页系统:

<?php
$p = $_GET['p'];

switch($p)
{
    case "start":
        $p = "pages/start.php";
        $currentPageId = 1;
    break;

    case "customers":
        $p = "pages/customers.php";
        $currentPageId = 2;
    break;

    default:
        $p = "pages/start.php";
        $currentPageId = 1;
    break;
}
?>

I want to set css class="active" to the menu item of the page i'm on. 我想将css class="active"设置为我所在页面的菜单项。

It works if I print <li> items like this: 如果我打印<li>像这样的项目,它将起作用:

<li><a href="?p=start" <?php if ($currentPageId == 1) {echo "class='active'";}else {} ?>>Start</a></li>

But I would like to use ternary operator instead. 但是我想改用三元运算符。 I tried this code but it doesn't work: 我尝试了这段代码,但没有用:

<li><a href="?p=start" <?php ($currentPageId == '1') ? 'class="active"' : '' ?>>Startsida</a></li>

Any idea why? 知道为什么吗?

EDIT So the problem was I was missing an echo . 编辑所以问题是我错过了echo Now let me extend the question a bit... 现在让我扩大一下问题...

I need to encapsulate my entire <ul> inside the <?php ?> tags. 我需要将整个<ul>封装在<?php ?>标记内。 So what I would like is something like this: 所以我想要的是这样的:

echo "<div id='nav'>";
 echo "<ul>";

   echo "<li><a href='?p=start' /* ternary operator to match if the page I'm on is equal to $currentPageId as defined in the paging system (above), if so set class='active' else do nothing*/>Start</a></li>;
   echo "<li><a href='?p=customers' /* ternary operator to match if the page I'm on is equal to $currentPageId as defined in the paging system (above), if so set class='active' else do nothing*/>Customers</a></li>;


 echo "</ul>";
echo "</div>";

I need to do this because I will display the links based on if statements.. "if user is admin display this link, else don't" ... Anyone got a solution? 我需要执行此操作,因为我将基于if语句显示链接。“如果用户是管理员,则显示此链接,否则不显示” ...有人解决方案吗?

You are missing an echo: 您缺少回声:

<li><a href="?p=start" <?php echo (($currentPageId == '1') ? 'class="active"' : '') ?>>Startsida</a></li>

That should do the trick. 这应该够了吧。


Addressing the second question: 解决第二个问题:

<?php
if($something == true) {
    echo "<div id='nav'>"."\n<br>".
            "<ul>"."\n<br>".
                '<li><a href="?p=start"'. (($currentPageId == '1') ? 'class="active"' : '') .'>Startsida</a></li>'."\n<br>".
                '<li><a href="?p=customers" '. (($currentPageId == '1') ? 'class="active"' : '') .' >Customers</a></li>'."\n<br>".
            "</ul>"."\n<br>".
            "</div>"."\n<br>";
}
?>

As others have pointed out, you were missing echo. 正如其他人指出的那样,您缺少回声。 I also wanted to point out that you don't even need a ternary operator in this case because you aren't doing anything in the else case: 我还想指出,在这种情况下,您甚至不需要三元运算符,因为在其他情况下,您什么也不做:

<li><a href="?p=start" <?php if ($currentPageId == '1') echo 'class="active"'; ?>>Startsida</a></li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM