PHP函数创建嵌套的ul

PHP函数创建嵌套的ul

本文介绍了PHP函数创建嵌套的ul li?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将小型CMS附加到我正在创建的网站。但是我遇到了一个小问题。 CMS使用PHP函数插入菜单,这些PHP函数创建HTML。我希望使用的特定函数(treemenu)创建一个嵌套的ul li,然后可以用于下拉菜单。然而,嵌套的ul li的结构如下:

I am attemptting to attach a small CMS to a website I am creating. However I have come across a small problem. The CMS uses PHP functions for inserting menus, these PHP functions create the HTML. The particular function I wish to use (treemenu) creates a nested ul li that can then be used for a drop down menu. However the nested ul li is structured like so:

<li>Projects (Menu Level 1)</li>
    <ul>
        <li>Project 1 (Menu Level 2)</li>
        <li>Project 2 (Menu Level 2)</li>
        <li>Project 3 (Menu Level 2)</li>
    </ul>
<li>News (Menu Level 1)</li>
<li>Contact (Menu Level 1)</li>

在CSS中创建下拉菜单时,我相信菜单级别1 li应该:

When creating a drop down menu in CSS I believe the Menu Level 1 li should wrap its children like so:

<li>Projects (Menu Level 1)
    <ul>
        <li>Project 1 (Menu Level 2)</li>
        <li>Project 2 (Menu Level 2)</li>
        <li>Project 3 (Menu Level 2)</li>
    </ul>
</li>
<li>News (Menu Level 1)</li>
<li>Contact (Menu Level 1)</li>

我从来没有使用过PHP,因此不知道如何改变函数来完成以上。我希望这将是一个简单的改变。下面是输出第一个示例结构的PHP函数:

I have never before worked with PHP and therefore would not know how to alter the function in order to accomplish the above. I would hope it would be a simple change. Below is the PHP function that outputs the first example structure:

function treemenu($generat=0) {
global $pagenum, $menu, $selected, $extension, $set;
$count=0;
$out="\n";
$intend=0;
while($menu[$count][0] != "") {
    if(strpos($menu[$count][3],"#") === false) {
    if($menu[$count][2]=="0" && $intend==2) {
        $intend--;
        $out.="</ul>\n";
    }
    if($menu[$count][1]=="0" && $intend==1) {
        $intend--;
        $out.="</ul>\n";
    }
    if($menu[$count][1]!="0" && $intend<1) {
        $intend=1;
        $out.="<ul>\n";
    }
    if($menu[$count][2]!="0" && $intend<2) {
        $intend=2;
        $out.="<ul>\n";
    }
    $out.="<li class=\"LNE_menu\"><a ";
    if($menu[$count][4]==$selected['name'])
        $out.= 'class="selected" ';
    if(strpos($menu[$count][3],"*"))
        $out.='href="'.str_replace("*", "",$menu[$count][3]).'">';
    elseif($generat)
        $out.='href="'.$menu[$count][3].".".$set['extension'].'">';
    else
        $out.='href="'.$set['indexfile'].'?page='.$menu[$count][3].'">';
    $out.=$menu[$count][4]."</a></li>\n";
    }
    $count++;
}
return $out;
}

任何人都可能指向正确的方向,

Could anyone possibly point me in the right direction as to how to make the closing li tag of a level 1 menu item wrap the ul immediately after, as in the second example?

推荐答案

这将是一个很好的示例使用递归。数组(其中包含子数组)定义每个级别,函数循环,每当它找到要处理的新数组时调用它自己。只要函数适当地清理(关闭< / li>&< / ol> ),就大部分是自动的。

This would be a excellent example of the use of recursion. An array (with sub-arrays within it) defines each level, and a function loops, calling itself whenever it finds a new array to process. As long as the function cleans up appropriately (closing the </li> & </ol>), it's largely automatic.

<?php
// I know which function I'd rather write....
$tree = array('Projects (Menu Level 1)',
              array('Project 1 (Menu Level 2)',
                    'Project 2 (Menu Level 2)',
                    'Project 3 (Menu Level 2)'),
              'News (Menu Level 1)',
              'Contact (Menu Level 1)');

// now quake beneath the power of this fully operational recursive function!
function olLiTree($tree)
{
    echo '<ul>';
    foreach($tree as $item) {
        if (is_array($item)) {
            olLiTree($item);
        } else {
            echo '<li>', $item, '</li>';
        }
    }
    echo '</ul>';
}
olLiTree($tree);  // kick off from the top level

这篇关于PHP函数创建嵌套的ul li?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:48