在CodeIgniter解析器类

时间:2015-05-07 21:42:13

标签: php arrays codeigniter

我的网站有一个导航菜单,我使用一系列链接来构建CodeIgniter的解析器类。当我想添加下拉子菜单时,我的问题就出现了。第一级变量对没有问题,除非它始终在列表项中显示子ul元素,无论是否存在子菜单链接。

当我使用这个数组时,子菜单的ul元素被正确设置,但子菜单的链接都没有解析,因为它缺少额外的级别。如何修改CI解析器类以解析额外级别的数组?

这是我的菜单的php数组:

$data['navigation_links'] = array(
            array(
                'href' => site_url('home'),
                'title' => 'My Home Page',
                'target' => '',
                'text' => 'Home',
                'submenu' => array()
                ),
            array(
                'href' => site_url('blog'),
                'title' => 'My Blog',
                'target' => '',
                'text' => 'Blog',
                'submenu' => array()
            ),
            array(
                'href' => site_url('portfolio'),
                'title' => 'My Portfolio',
                'target' => '',
                'text' => 'Portfolio',
                'submenu' => array(
                    'submenu_links' => array(
                        array(
                            'sub_href' => site_url('portfolio/gallery'),
                            'sub_title' => 'Photography Gallery',
                            'sub_target' => '',
                            'sub_text' => 'Photography Gallery'
                        ),
                        array(
                            'sub_href' => site_url('portfolio/web-projects'),
                            'sub_title' => 'Web Projects',
                            'sub_target' => '',
                            'sub_text' => 'Web Projects'
                        )
                    )
                )
            ),
            array(
                'href' => site_url('services'),
                'title' => 'My Services',
                'target' => '',
                'text' => 'Services',
                'submenu' => array()
            ),
            array(
                'href' => site_url('resume'),
                'title' => 'My Resume',
                'target' => '',
                'text' => 'Resume',
                'submenu' => array()
            ),
            array(
                'href' => site_url('about'),
                'title' => 'About Me',
                'target' => '',
                'text' => 'About',
                'submenu' => array()
            ),
            array(
                'href' => site_url('contact'),
                'title' => 'Contact Me',
                'target' => '',
                'text' => 'Contact',
                'submenu' => array()
            )
        );

这是视图文件的菜单部分:

<nav class="container">
                        <ul>
                            {navigation_links}
                            <li><a href="{href}" title="{title}"{target}>{text}</a>
                                {submenu}
                                <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
                                    {submenu_links}
                                    <li><a href="{sub_href}" title="{sub_title}"{sub_target}>{sub_text}</a></li>
                                    {/submenu_links}
                                </ul>{/submenu}</li>
                            {/navigation_links}
                        </ul>
                    </nav>

这是HTML输出:

<nav class="container">
                    <ul>

                        <li><a href="http://localhost/scotthlacey/index.php/home" title="My Home Page">Home</a>
                            </li>

                        <li><a href="http://localhost/scotthlacey/index.php/blog" title="My Blog">Blog</a>
                            </li>

                        <li><a href="http://localhost/scotthlacey/index.php/portfolio" title="My Portfolio">Portfolio</a>

                            <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
                                {submenu_links}
                                <li><a href="{sub_href}" title="{sub_title}"{sub_target}>{sub_text}</a></li>
                                {/submenu_links}
                            </ul></li>

                        <li><a href="http://localhost/scotthlacey/index.php/services" title="My Services">Services</a>
                            </li>

                        <li><a href="http://localhost/scotthlacey/index.php/resume" title="My Resume">Resume</a>
                            </li>

                        <li><a href="http://localhost/scotthlacey/index.php/about" title="About Me">About</a>
                            </li>

                        <li><a href="http://localhost/scotthlacey/index.php/contact" title="Contact Me">Contact</a>
                            </li>

                    </ul>
                </nav>

1 个答案:

答案 0 :(得分:0)

尝试将另一级数组包装到submenu_links。因为您的数据(您要迭代的每个数组)必须是多维数组才能通过解析器进行迭代。

$data['navigation_links'] = array(
    array(
        'href' => site_url('home'),
        'title' => 'My Home Page',
        'target' => '',
        'text' => 'Home',
        'submenu' => array()
        ),
    array(
        'href' => site_url('blog'),
        'title' => 'My Blog',
        'target' => '',
        'text' => 'Blog',
        'submenu' => array()
    ),
    array(
        'href' => site_url('portfolio'),
        'title' => 'My Portfolio',
        'target' => '',
        'text' => 'Portfolio',
        'submenu' => array(
            array(
                'submenu_links' => array(
                    array(
                        'sub_href' => site_url('portfolio/gallery'),
                        'sub_title' => 'Photography Gallery',
                        'sub_target' => '',
                        'sub_text' => 'Photography Gallery'
                    ),
                    array(
                        'sub_href' => site_url('portfolio/web-projects'),
                        'sub_title' => 'Web Projects',
                        'sub_target' => '',
                        'sub_text' => 'Web Projects'
                    )
                )
            )
        )
    ),
    array(
        'href' => site_url('services'),
        'title' => 'My Services',
        'target' => '',
        'text' => 'Services',
        'submenu' => array()
    ),
    array(
        'href' => site_url('resume'),
        'title' => 'My Resume',
        'target' => '',
        'text' => 'Resume',
        'submenu' => array()
    ),
    array(
        'href' => site_url('about'),
        'title' => 'About Me',
        'target' => '',
        'text' => 'About',
        'submenu' => array()
    ),
    array(
        'href' => site_url('contact'),
        'title' => 'Contact Me',
        'target' => '',
        'text' => 'Contact',
        'submenu' => array()
    )
);

希望它对你有用。