0

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

<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>

我从数据库中选择 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
        )

)

第二个数组有类别

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

从这两个数组我都没有到达创建列表结构
$nav = new navigation($myDataArray);
$nav->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;
    }
}
4

1 回答 1

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){ ... }
于 2013-07-15T00:14:02.160 回答