用php和mysql生成的无序子列表的输出

时间:2015-09-11 06:22:18

标签: php mysql html-lists

我成功地使用数据库中父表的值创建了一个无序列表。每个父列表应该具有一个子无序列表,基于它是否在子表中具有值。我创建了一个实现此目的的类,但是,子列表似​​乎打破了父无序列表的输出。我还没理解为什么!我发布了根据父ID打印出父列表和子列表的方法的代码;我怀疑这个错误来自我的foreach(s),但我不确定,我可以发布整个代码,但我不确定这是否正确;

     public function construct_list($type,$parent = true){

    if($parent){

        $this->load_parent_array = $this->load_parent_list();

        switch($type){

            case 'ul':

                $this->list_string = '';

                foreach($this->load_parent_array as $o_keys => $o_vals):

                    $this->list_string .= '<li>';

                    foreach($o_vals as $k_vals => $_vals):

                        if($k_vals === 'parent_id'){

                            $this->list_string .= '
                                                    <label class="inline-label">
                                                    <input data-toggle="child-id-'.$_vals.'" type="checkbox" name="parent_cat[]" value="'.$_vals.'" class="unicorn" />
                                                ';

                        }

                        if($k_vals === 'parent_category_names'){

                                $this->list_string .= '<span class="check-label">'.$_vals.'</span>
                                                    </label>';

                        }

                        //Inner Unordered list code

                            $this->load_child_array = $this->load_child_list($_vals);

                            if(!empty($this->load_child_array)){

                                if($k_vals === 'parent_id'){

                                    $this->list_string .= '<ul id="child-id-'.$_vals.'" class="child-category">';

                                        foreach($this->load_child_array as $o_keys => $o_vals):

                                            $this->list_string .= '<li>';


                                            foreach($o_vals as $v => $f){

                                                if($v === 'child_category_name'){

                                                    $this->list_string .= '<label class="inline-label">
                                                                        <input type="checkbox" name="child_category[]" value="'.$f.'" />
                                                                        <span class="check-label">'.$f.'</span>
                                                                    </label>';
                                                }

                                            }

                                            $this->list_string .= '</li>';

                                        endforeach; 

                                    $this->list_string .= '</ul>';

                                }

                            }

                        endforeach;

                    $this->list_string .= '</li>';

                endforeach;

            break;

            case 'select':

                $this->list_string = '<li>--Parent Category--</li>';

                foreach($this->load_parent_array as $o_keys => $o_vals):

                    foreach($o_vals as $k_vals =>$_vals):

                        #$this->list_string .= '<option value="';

                        if($k_vals === 'parent_category_slug'){

                            $this->list_string .= '<option value="'.$_vals.'">';    

                        }

                        if($k_vals === 'parent_category_names'){

                            $this->list_string .= $_vals.'</option>';

                        }



                    endforeach;

                endforeach;

            break;
        }

    }else{


    }

     return $this->list_string;
 }

0 个答案:

没有答案