怎么把数据库查询出的树结构的数据循环赋值给数组

WBOY
Release: 2016-06-13 12:35:27
Original
827 people have browsed it

如何把数据库查询出的树结构的数据循环赋值给数组?
//数据库
     id                      parentid                       name        status
03e782cd-0f00-11e3-bcf6-001d608a0834                                                                        总公司 1
49d15b3a-1798-11e3-991e-001d608a0834    55ac840c-0f01-11e3-bcf6-001d608a0834 设计科 1
49e24a33-1798-11e3-991e-001d608a0834    55ac840c-0f01-11e3-bcf6-001d608a0834 实施科 1
49f452aa-1798-11e3-991e-001d608a0834    55ac840c-0f01-11e3-bcf6-001d608a0834 质检科 1
5599eec4-0f01-11e3-bcf6-001d608a0834    f6597beb-0f00-11e3-bcf6-001d608a0834 市场部 1
55ac840c-0f01-11e3-bcf6-001d608a0834    f6597beb-0f00-11e3-bcf6-001d608a0834 研发部 1
55ca0dad-0f01-11e3-bcf6-001d608a0834    f6597beb-0f00-11e3-bcf6-001d608a0834 人事部 1
b5e86db9-0f01-11e3-bcf6-001d608a0834   f6667833-0f00-11e3-bcf6-001d608a0834 财务部 1
b5f69ff5-0f01-11e3-bcf6-001d608a0834    f6667833-0f00-11e3-bcf6-001d608a0834 经理部 1
b60bc9b5-0f01-11e3-bcf6-001d608a0834   f6667833-0f00-11e3-bcf6-001d608a0834 后勤部 1
f6597beb-0f00-11e3-bcf6-001d608a0834   03e782cd-0f00-11e3-bcf6-001d608a0834 一公司 1
f6667833-0f00-11e3-bcf6-001d608a0834   03e782cd-0f00-11e3-bcf6-001d608a0834 二公司 1
f6761c03-0f00-11e3-bcf6-001d608a0834   03e782cd-0f00-11e3-bcf6-001d608a0834 三公司 1
f683c3c7-0f00-11e3-bcf6-001d608a0834   03e782cd-0f00-11e3-bcf6-001d608a0834 四公司 1
f6955d9f-0f00-11e3-bcf6-001d608a0834    03e782cd-0f00-11e3-bcf6-001d608a0834 五公司 1
//php方法代码
 public function getTreeModel() {
        $sql = "select *from menutree where parentid is NULL";
        $quer = $this->db->query($sql);
        
        $row = $quer->result_array();
        $data=array();
        if ($quer->num_rows()>0) {
            //总公司
            $data=$this->getArray($row);
        }
        echo count($data)."
";
        return $data;
    }
//我这个循环赋值给数组有点问题。。。页面只输出了一二级,三四级都没输出,可是我var_dump($row3)都是有值的。下面这个方法望指点一下啊
    public function getArray($row) {
        $data=array();
        foreach ($row as $val) {
            
            $arr1['id'] = $val['id'];
            $arr1['menuname'] = $val['menuname'];
            
            $data[] = $arr1;
            
            $sql2 = "select *from menutree where parentid ='$val[id]'";
            $quer2 = $this->db->query($sql2);
            $row2 = $quer2->result_array();
            if ($quer2->num_rows()>0) {
                foreach ($row2 as $va2) {
                    $arr2['id'] = $va2['id'];
                    $arr2['menuname'] = $va2['menuname'];
                    
                    $data[] = $arr2;
                    
                     //echo $va2['menuname']."
";
                     $sql3 = "select *from menutree where parentid ='$va2[id]'";

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!