如何在PHP中进行动态下拉?

时间:2013-06-06 08:17:50

标签: codeigniter

我想在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 |
+---------------------------------------------------------------+

1 个答案:

答案 0 :(得分: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];
}