麻烦弄清楚如何使用foreach循环关闭标签

时间:2012-04-04 21:02:43

标签: php arrays codeigniter recursion foreach

我正在通过数据库建立一个类别管理器。以下PHP生成我将在下面粘贴的代码:

function generate_menu($parent, $menu_array, $result = NULL)
{
    $has_childs = FALSE;

    foreach($menu_array as $key => $value):
        if ($value['parent'] == $parent):
            $result .= '<li id="list_'.$value['id'].'" class="item">';
            $result .= '<div class="item">' . $value['name'] . '</div>';

            if ($has_childs):
                $has_childs = FALSE;
                $result .= '<ol>';
            else:
                $has_childs = TRUE;
                $result .= '</li>';
            endif; 

            $result .= $this->generate_menu($key, $menu_array);
        endif;
    endforeach;

    return $result;
}

这是它生成的HTML:

<li id="list_11" class="item">
    <div class="item">Real Estate</div>
</li>
<li id="list_12" class="item">
    <div class="item">Home Improvements</div>
    <ol>
        <li id="list_13" class="item">
            <div class="item">Interior</div>
        </li>
        <li id="list_14" class="item">
            <div class="item">Exterior</div>
            <ol>
                <li id="list_15" class="item">
                    <div class="item">Exterior Subcat</div>
                </li>

所以我可以关闭<li>标签,如果它是父母,但如果它是带有父母的项目,则<ol>会输入等式。就在那时我不知道如何关闭它们。我不确定将</li></ol>放在哪里。我尝试过的所有东西都不起作用。

以下是应该的样子:

<li id="list_11" class="item">
    <div class="item">Real Estate</div>
</li>
<li id="list_12" class="item">
    <div class="item">Home Improvements</div>
    <ol>
        <li id="list_13" class="item">
            <div class="item">Interior</div>
        </li>
        <li id="list_14" class="item">
            <div class="item">Exterior</div>
            <ol>
                <li id="list_15" class="item">
                    <div class="item">Exterior Subcat</div>
                </li>
            </ol>
        </li>
    </ol>
</li>

修改

以下是传递数组和生成菜单的代码:

$query = $this->db->get('categories');

foreach($query->result_array() as $row):
    $menu_array[$row['id']] = array('name' => $row['name'], 'parent' => $row['parent'], 'id' => $row['id']);
endforeach;

echo '<ol class="sortable">';
echo $this->tasks->generate_menu(0, $menu_array);
echo '</ol>';

这是$ menu_array的print_r:

Array
(
    [11] => Array
        (
            [name] => Real Estate
            [parent] => 
            [id] => 11
        )

    [12] => Array
        (
            [name] => Home Improvements
            [parent] => 
            [id] => 12
        )

    [13] => Array
        (
            [name] => Interior
            [parent] => 12
            [id] => 13
        )

    [14] => Array
        (
            [name] => Exterior
            [parent] => 12
            [id] => 14
        )

    [15] => Array
        (
            [name] => Exterior Subcat
            [parent] => 
            [id] => 15
        )

)

2 个答案:

答案 0 :(得分:0)

我已经想到了这一个。我让事情变得复杂了。

function generate_menu($parent, $menu_array, $result = NULL)
{       
    foreach($menu_array as $key => $value):
        if ($value['parent'] == $parent):
            $result .= '<li id="list_'.$value['id'].'" class="item">';
            $result .= '<div class="item">' . $value['name'] . '</div>';
            $result .= '<ol>';
            $result .= $this->generate_menu($key, $menu_array);
            $result .= '</li></ol>';
        endif;
    endforeach;

    return $result;
}

答案 1 :(得分:0)

为了正确使用递归函数,值数组需要反映层次结构,如下所示:

array(
[11] => "Real Estate",
[12] => array(
    [0] => "Home Improvements",
    [13] => "Interior",
    [14] => "Exterior"
    ),
[15] => "Exterior Subcat"
)

该函数现在可以迭代11,正常添加列表项。在12它会遇到一个数组而不是一个字符串(使用gettype($value)),所以不是添加一个列表项而是再次调用它自己 - 传递它刚刚找到的数组作为参数。这在理论上允许无限数量的等级。

function make_menu($menu_array) {

    $result = "<ol>";

    foreach($menu_array as $k=>$v)
    {
        $result .= '<li id="list_'.$k.'" class="item">';

        if(gettype($v) == 'array')
        {
            $result .= '<div class="item">' . $v[0] . '</div>';
            unset($v[0]);
            $result .= make_menu($v);
        }
        elseif(gettype($v) == 'string')
        {
            $result .= '<div class="item">' . $v . '</div>';
        }

        $result .= '</li>';
    }
    $result .= "</ol>";
    return $result;
}