Cześć! Walczę ze stworzeniem drzewka i się już zapętliłem (IMG:
style_emoticons/default/sciana.gif)
O co chodzi?
Ano mam coś takiego:
<?
$cats = new cats();
$cats->GenerateArray();
class cats{
public $CArray = array(); function __construct(){
'id' => 1,
'name' => 'Realizacje',
'parent_id' => 0
);
(
'id' => 2,
'name' => 'Wroc?aw',
'parent_id' => 1
);
(
'id' => 3,
'name' => 'Realizacje 2',
'parent_id' => 2
);
(
'id' => 4,
'name' => 'Polanica',
'parent_id' => 0
);
(
'id' => 5,
'name' => 'Podkategoria 3',
'parent_id' => 3
);
(
'id' => 6,
'name' => 'PodPodkategoria 4',
'parent_id' => 5
);
$this->cats = $array;
}
function GenerateArray($parent=0, $level=0, $prefix=''){
if (!empty($this->cats)){
foreach ($this->cats as $item){
if ($item['parent_id']==$parent){
if($item['parent_id']==0)
$this->CArray[$item['id']] = array('id'=>$item['id'], 'name'=>$item['name'],
'parent_id'=>$item['parent_id'],
);
else $this->CArray[$item['parent_id']]['childs'][] = array('id'=>$item['id'], 'name'=>$item['name'],
'parent_id'=>$item['parent_id'],
);
$this->ids2[] = $item['id'];
$this->GenerateArray($item['id'],$level+1, $space);
}
}
}
}
}
}
i chcę, aby $this->CArray miał strukturę:
$this->CArray[ID] = (
['name'] => nazwa kategorii
'childs'=> array()//tutaj mają być "dzieci" kategorii
);
Czyli żeby powstało coś takiego:
(IMG:
http://irzo.pl/files/cats_tree.png)
Pozdrawiam!
problem jest z tym, żeby zagnieżdżał się w zmiennej
$CArray