Skip to content

Instantly share code, notes, and snippets.

@steppefox
Created November 4, 2013 11:31
Show Gist options
  • Save steppefox/7301301 to your computer and use it in GitHub Desktop.
Save steppefox/7301301 to your computer and use it in GitHub Desktop.
Yii snippet for recursive finding categories
function findCatalogCategory($arr=array(),$id=0,$s=''){
$ms=CatalogCategory::model()->findAll(array('condition'=>'`parent_id`='.$id,'order'=>'title','select'=>array('id','title')));
foreach ($ms as $m){
$arr[]=array('title'=>$s.$m->title,'id'=>$m->id);
if(CatalogCategory::model()->count(array('condition'=> '`parent_id`='.$m->id,'order'=>'title','select'=>array('id','title')))>0){
$arr=findCatalogCategory($arr,$m->id,($s.$m->title.' - '));
}
}
return $arr;
}
$catsList=findCatalogCategory(
array(0=>array('title'=>'Веберите категорию','id'=>0)),0
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment