0

我想在 CI 中进行动态下拉。我已经在 CI 中通过直接传递模型中的值来完成它,它的设计是通过连接 html 来实现的。但是现在我想要它没有 html,即函数只应该返回数组...我之前的功能是这样的...

function category() {
        $sql = $this->db->query("select * from ss_category where parent_id='0'");
         $result = $sql->result();
         //print_r($result);die();
         $output = array();
         foreach ($result as $ra) {
             $output=$this->get_child($ra->cat_id);
                    }
        //print_r($output);die();
        return $output;
    }

    function get_child($parent_id) {
        //echo "select * from ss_category where parent_id=" . $parent_id;
        $sql = $this->db->query("select * from ss_category where parent_id=" . $parent_id);
        $r = $sql->result();

        if (!empty($r)) {
            foreach ($r as $s) {
                    $this->get_child($s->cat_id);
            }
            //print_r($child);
         }
         //print_r($r);
     }

我想做这样的下拉菜单..

-parent 1
       -child 1
           -subchild 1
       -child 2
       -child 3
          -subchild 1
    -parent 2
      -child 1

我的表结构是这样的..

+---------------------------------------------------------------+
| cat_id category_name cat_slug     parent_id level description |
+---------------------------------------------------------------+
| 1      Elecotronics  Electronics  1               description |
+---------------------------------------------------------------+
4

1 回答 1

0
function parent_sort($categories)
{
    foreach($categories as $item)
        $childs[$item->parent_id][] = $item;

    foreach($categories as $item) 
        if (isset($childs[$item->category_id]))
            $item->childs = $childs[$item->category_id];

    return $childs[0];
}
于 2013-06-06T15:29:01.030 回答