drop down menu with "tree" data

I have a table categories.The records can have childs categories.

So I want to do a dropdown list with categories and each category to have below her the subcategories

I can create a function at model for example


public function returnchilds()

        {$sql="SELECT cat_id,cat_code FROM cats  WHERE parent_id is null";

$command=Yii::app()->db->createCommand($sql);

$rows=$command->queryAll(); //do a loop and get the subcategories

return $rows;

            

        }

But how can I merge the arrays of categories?array_merge returns a new array with integer keys starting at 0 and increases by 1 for each value.

I make this code but it does not work.Can be an improvement?


public function returnchilds($id=null,$code='',$allrows=array())

        { $all=array ();

           if ($id==null)

            $sql="SELECT cat_id,CONCAT('$code',cat_code) as cat_code FROM cats  WHERE parent_id is null";

        else

         $sql="SELECT cat_id,cat_code FROM cats  WHERE parent_id=$id";

$command=Yii::app()->db->createCommand($sql);

$dataReader=$command->query();

foreach ($dataReader as $row)

{$all= $all+$row;

$all= $all+$this->returnchilds($row['cat_id'], $code." ",$allrows);

}$allrows=$allrows+$all;

return $allrows;

            

        }

}

http://www.yiiframework.com/doc/cookbook/61/

[edit] with CHtml::listData seems to be ok

This code works but the array is not for the CHtml::activeDropDownList


  $mymodel =cats::model()->findByPk(4);

$items = $mymodel->getListed();//$a= CHtml::listData($items,'id','code');

        print_r($items);echo CHtml::activeDropDownList($model, 'cat_id', $items);

public function getListed() {

     $subitems=array();

    if($this->childs) foreach($this->childs as $child) {

      $subitems+= $child->getListed();

    }

    $returnarray[]= array('id' => $this->cat_id, 'code' => $this->cat_code);

   if($subitems != array()) $returnarray = array_merge($returnarray, $subitems);

    return $returnarray ;

  }

}


Array([0] => Array ( [id] => 4 [code] => test  ) [1] => Array  ( [id] => 6  [code] => simera ))

<select name="cats[cat_id]" id="cats_cat_id">

<optgroup label="0">

<option value="id">4</option>

<option value="code">test</option>

</optgroup>

<optgroup label="1">

<option value="id">6</option>

<option value="code">simera</option>

</optgroup>

</select>

in your model:




public static $category_list;


public function getCategories(){

        $categories = Categories::model()->findAll();

        self::$category_list[0] = '';

        foreach ($categories as $value){

            if($value->id != $self_id && $value->parent_id == 0){

                self::$category_list[$value->id] = $value->category;

                self::dropDownTree($categories, $value->id);

            }

        }

        return self::$category_list;

    }


    protected function dropDownTree($array, $parent_id){

        foreach ($array as $item){

            if($item->id != $self_id && $item->parent_id == $parent_id){

                self::$category_list[$item->id] = $item->category;

                self::dropDownTree($array, $item->id);

            }

        }

    }



and in controller:




$form->dropDownList($model,'parent_id', $model::getCategories();