从数据库生成动态ul li结构

时间:2013-07-14 20:42:38

标签: php

我需要一个来自数据库的动态无序列表

<div class="box">
        <h2>Auswahl</h2>
            <div class="auswahl">
                <ul>
                    <li>
                        <a href="#">Kat a</a>
                        <ul class="submenuAuswahl">
                            <li><a href="#">a</a></li>
                            <li><a href="#">b</a></li>
                            <li><a href="#">c</a></li>
                            <li><a href="a">d</a></li>
                            <li><a href="#">e</a></li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">Kat b</a>
                         <ul class="submenuAuswahl">
                            <li><a href="#">1</a></li>
                            <li><a href="#">2</a></li>
                            <li><a href="#">3</a></li>
                            <li><a href="#">4</a></li>
                        </ul>
                    </li>
                    <li>
                        <a href="#">Kat c</a>
                         <ul class="submenuAuswahl">
                            <li><a href="#">I</a></li>
                            <li><a href="#">II</a></li>
                            <li><a href="#">III</a></li>
                            <li><a href="#">IV</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>

我从DB中选择3个表并生成array_merge
 $ data = array_merge($ z,$ s,$ k);

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => a
        )

    [1] => Array
        (
            [id] => 2
            [name] => b
        )

    [2] => Array
        (
            [id] => 3
            [name] => c
        )

    [3] => Array
        (
            [id] => 1
            [name] => 1
        )

    [4] => Array
        (
            [id] => 2
            [name] => 1
        )

    [5] => Array
        (
            [id] => 1
            [name] => I
        )

    [6] => Array
        (
            [id] => 2
            [name] => II
        )

    [7] => Array
        (
            [id] => 3
            [name] => III
        )

)

第二个数组有Kategoire

Array
    (
        [id_a] => kat a
        [id_b] => kat b
        [id_c =>  kat c
    )

从两个阵列中我都没有到达创建列表结构
$ nav =新导航($ myDataArray);
$ nav-&GT; createMenu($ myKategorieArray);

<?php

class navigation {
    private $menu     = array();
    private $data       = array();


    public function __construct($d){
        $this->data   =   $d;
    }

    public function createMenu($aus){

        $this->menu .= '<div class="box">';
        $this->menu .='<h2>Auswahl</h2>';
        $this->menu .='<ul><li>';
        $key = '';
        foreach($aus as $k => $v){

            if($key != $k){
                $this->menu .='<a href="#">'.$k.'</a>';
                $this->createList($k);
            }
            $key = $k;
        }
        $this->menu .='</ul></li>';
    }


    public  function createList($key){

        $this->menu .= "<ul>";

        foreach($this->data as $list => $in){

            foreach($in as $k => $v){
                $this->menu .=  '<li class="hidden">'. $k .'</li><li><a href="#">'.$v.'</a></li>';
            }
            $this->menu .=  "</ul>";
        }
    }


    public function getMenu(){
        return $this->menu;
    }

    public function getData()
    {
        return $this->data;
    }
}

1 个答案:

答案 0 :(得分:0)

您的$data数组对我来说似乎毫无用处 - 没有信息哪个元素属于哪个类别。

您可以通过这种方式创建$data

$data[id_a] = $z;
$data[id_b] = $s;
$data[id_c] = $k;

而不是createList($key),您可以使用$key

foreach($this->data[$key] as $list => $in){ ... }