在html类中调用if语句

时间:2015-11-27 05:44:55

标签: php html css dynamic navigation

我试图创建一个动态导航,其中使用简单的if语句检查当前页面是否等于当前url。这将回显"选择"如果这些东西匹配,在html类中。当我测试我的代码时,我看到整个php语句用html打印出来。我错过了什么,或者这可能是我试图解决的问题?

- 谢谢你

<?php
    $menu = array(
        'home' => array('text' => 'home', 'url' => '/'),
        'about'  => array('text' => 'about',  'url' => '/about'),
        'contact' => array('text' => 'contact', 'url' => '/contact'),
    );

    $navbar = "navbar";

    function GenerateMenu($items, $class) {

        $currentpage = $_SERVER['REQUEST_URI'];
        $selected = "selected";

        $html = "<nav class = '$class'>\n";

        foreach ($items as $key => $item) {

            $html .= "\t<a id = '{$item['text']}' class = '<?php if(preg_match({$item['url']}, $currentpage)) echo $selected; ?> ' href = '{$item['url']}'> {$item['text']} ";
        }
        $html .= "</a>\n"
            . "</nav>\n";


        return $html;

    };
    echo GenerateMenu($menu, $navbar);

    ?>

4 个答案:

答案 0 :(得分:0)

使用以下代码替换您的代码:

<?php
    $menu = array(
        'home' => array('text' => 'home', 'url' => '/'),
        'about'  => array('text' => 'about',  'url' => '/about'),
        'contact' => array('text' => 'contact', 'url' => '/contact'),
    );

    $navbar = "navbar";

    function GenerateMenu($items, $class) {

        $currentpage = $_SERVER['REQUEST_URI'];

        $html = "<nav class = '".$class."'>\n";

        foreach ($items as $key => $item) {
            $selected = (preg_match({$item['url']}, $currentpage)) ? "selected" : "";
            $html .= "\t<a id = '".{$item['text']}."' class = '".$selected."' href = '".{$item['url']}."'> ".{$item['text']}." </a>\n";
        }

        $html .= "</nav>\n";

        return $html;

    };

    echo GenerateMenu($menu, $navbar);

    ?>

答案 1 :(得分:0)

试试这个..

$html .= "\t<a id = '{$item['text']}' class = '".if(preg_match({$item['url']}, $currentpage)) echo $selected;."' href = '{$item['url']}'> {$item['text']} ";

并且对于应用活动类,为什么不使用下面的jquery ..

var loc = window.location.pathname;

   $('#nav').find('a').each(function() {
     $(this).toggleClass('active', $(this).attr('href') == loc);
  });

希望这会有所帮助..

答案 2 :(得分:0)

将您的功能更改为此

function GenerateMenu($items, $class) {

    $currentpage = $_SERVER['REQUEST_URI'];

    $html = "<nav class = '$class'>\n";

    foreach ($items as $key => $item) {

        $selected = '';
        if(preg_match({$item['url']}, $currentpage))
        {
            $selected = "selected";
        }

        $html .= "\t<a id='{$item['text']}' class='{$selected}' href='{$item['url']}'> {$item['text']} ";
    }
    $html .= "</a>\n"
        . "</nav>\n";


    return $html;
};

答案 3 :(得分:0)

找到了一个有效的解决方案。

<?php
$menu = array(
    'home' => array('text' => 'home', 'url' => '/'),
    'about'  => array('text' => 'about',  'url' => '/about/'),
    'contact' => array('text' => 'contact', 'url' => '/contact/'),
);

$navbar = "navbar";

function GenerateMenu($items, $class) {

    $currentpage = $_SERVER['REQUEST_URI'];


    $html = "<nav class = '$class'>\n";

    foreach ($items as $item) {
        //$selected = (preg_match('$item['url']', $currentpage)) ? 'selected' : null;
        if($item['url'] == $currentpage)
        {
            $selected = 'selected';
        }
        else
        {
            $selected = " ";
        }
        $html .= "\t<a id = '{$item['text']}' class = '{$selected}' href = '{$item['url']}'> {$item['text']} ";
    }
    $html .= "</a>\n"
        . "</nav>\n";


    return $html;

};
echo GenerateMenu($menu, $navbar);

?>