如何将Parent和Sub-menu类设置为活动

时间:2018-05-28 12:13:00

标签: php css arrays json

当前问题:将父级和子菜单类设置为活动状态。

Bassically,我有一个带有当前页面布局的json数组。我想遍历数组并查找它是否与当前页面匹配。由于某种原因,它根本不匹配,当我确实得到它匹配时,输出的页面名称不是"活动"或" _active" ?

任何更好的方法都会非常感激:)

json页面数组:

{
    "Pages":{
        "Home":"Dashboard",
        "USD Market":{
            "Current-Trades":"USD-Current-Trades",
            "Exchange-Trades":"USD-Exchange-Trades"
        }
    }
}
页眉中的

<?php   
    $directoryURI = $_SERVER['REQUEST_URI'];
    $path = parse_url($directoryURI, PHP_URL_PATH);
    $components = explode('/', $path);
    $pagename = $components[1];
?>

功能:

    public function pages($pagename)
{
    if ($pagename == array($config['Pages']))
    {
        print 'yes';
    } else {
        print 'no';
    }
}

菜单:

            <div class="menu">
            <ul class="list">
                <li class="header">MAIN NAVIGATION</li>
                <li class="<?php print $dash->pages($pagename); ?>">
                    <a href="/Dashboard">
                        <i class="material-icons">home</i>
                        <span>Home</span>
                    </a>
                </li>
                <li>
                    <a href="javascript:void(0);" class="menu-toggle">
                        <i class="material-icons">trending_down</i>
                        <span>USD ($) - Market Trades</span>
                    </a>
                    <ul class="ml-menu">
                        <li class="<?php print $dash->pages($pagename); ?>">
                            <a href="/USD-Current-Trades">
                                <span>Current Trades</span>
                            </a>
                        </li>
                        <li>
                            <a href="javascript:void(0);">
                                <span>Exchange Trades</span>
                            </a>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>

感谢您提供的任何输入:)

1 个答案:

答案 0 :(得分:1)

public function pages()中,您可以查看:

if ($pagename == array($config['Pages']))

$config永远不会在该方法中定义,也不会传入,因此它始终为false。如果查看error_log,如果设置了error_reporting,则可能会看到通知。 (将error_reporting设置为-1!)

我猜你的意思是json_decode($json, true)在某个地方,然后将其作为第二个参数传递。

<?php

$pages = json_decode('{
    "Pages":{
        "Home":"Dashboard",
        "USD Market":{
            "Current-Trades":"USD-Current-Trades",
            "Exchange-Trades":"USD-Exchange-Trades"
        }
    }
}', true);

class Z
{
    private $pages;

    private $currentPage;

    public function setConfig($currentPage, array $config)
    {
        $this->currentPage = $currentPage;
        $this->pages = [];
        $this->addToPageList($config['Pages']);
    }

    public function pages($linkName)
    {
        return $this->pages[$linkName];
    }

    public function addToPageList(array $pages)
    {
        foreach ($pages as $key => $page) {
            if (is_array($page)) {
                $this->addToPageList($page);
            } else {
                $active = ($page == $this->currentPage) ? 'active' : 'inactive';
                $this->pages[$page] = $active;
            }
        }
    }
}

$z = new Z();
$z->setConfig('USD-Exchange-Trades', $pages); // current page, page config
echo $z->pages('USD-Exchange-Trades') . "\n";
echo $z->pages('USD-Current-Trades') . "\n";
echo $z->pages('Dashboard') . "\n";

输出:

active 
inactive 
inactive

在这里玩https://3v4l.org/PtAiF