bonjour, je te présente ici un systeme identique que j'ai mis en place pour personaliser ma vresion du tuto MVC de A à Z de grafickart et qui
marche très bien. tu as juste à l'adapter à ton code.
public function getMenu()
{
$d = array();
/* Remplacer ceci par ta requete sql
$this->loadModel('Menu');
if(!$d = $this->Menu->fetchAll()) return false;*/
//la requete me retourne le resultat sous forme d'objet cette fonction va le transformer en tableau
$toArr=function()use($d)
{
if(!is_object($d) and !is_array($d))
return $d;
if(is_object($d)) $d = get_object_vars($d);
return array_map($toArr, $d);
};
//ici je le transforme
$d = $toArr();
//cette fonction va pour chaque parent créer un sous tableau 'childs' et y placer tous les elements enfants
$prepare=function($arr)
{
krsort($arr);
foreach ($arr as $k => &$item):
if (is_numeric($item'parentid'])):
$parent = $item'parentid'];
if (empty($arr$parent]'childs'])) $arr$parent]'childs'] = array();
array_unshift($arr$parent]'childs'],$item);
unset($arr$k]);
endif;
endforeach;
ksort($arr);
return $arr;
};
$d=$prepare($d);
//$this->set($d);
return $d;
}
et l'affichage, tu as ceci:
/**
*
*
**/
public function buildMenu($menu, $attributes='')
{
// Were any attributes submitted? If so generate a string
if (is_array($attributes)):
$atts = array();
foreach ($attributes as $key => $val)
$atts]= "$key=\"$val\"";
$attributes = implode(' ',$atts);
endif;
$html = (is_string($attributes) and strlen($attributes) > 0) ? '<ul '.$attributes.'>':'<ul>';
foreach ($menu as $item)
{
$html .= '<li><a href="'.Router::url($item->url).'" title="'.$item->name.'" >'.$item->name.'</a>';
if (isset($item'childs']) and !empty($item'childs'])) $html .= $this->buildMenu($item'childs'],$attributes);
$html .= '</li>';
}
return $html .= '</ul>';
}