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;
}
}