Fonction récursive et indentation des éléments

Nouveau WRInaute
Bonjour,

Je travaille actuellement pour l'un de mes clients sur une gestion approfondie des pages de contenu.

Je développe mon propre CMS que j'adapte au fil des demandes.

La gestion approfondie des pages inclue la gestion parent-enfant. Ce qui permet de définir telle ou telle page comme étant une sous page.

Exemple : La page « Réinstallation d'un OS » pourrait être une sous-page de « Dépannage informatique ».

Ma fonction récursive fonctionne au poil. Elle parcourt bien l'ensemble de l'arborescene et me retourne un array() de toutes mes pages. Cependant, elle ne gère pas l'indentation...

Comment gérer l'indentation dans cette fonction pour que les noms des sous-pages soient indentés d'un " ", que les sous-sous-pages soient indentées d'un "  ", ...

Code:
// Liste pages, nested pages
function pages_list($parent = 0, &$catlistids = array(), &$indent = '') {
	global $db;
	
	$pages_query = "SELECT pages_id, pages_title, parent_id FROM ".TABLE_PAGES." WHERE parent_id='".$parent."' ORDER BY sort_order ASC";
	$pages = $db->prepare($pages_query);
	$pages->Execute();
	if ($pages->RowCount() > 0) {
		$pages->setFetchMode(PDO::FETCH_ASSOC);
			
		while ($pages_res = $pages->fetch()) {
		
			$catlistids[] = array(	'parent' => $pages_res['parent_id'],
									'id' => $pages_res['pages_id'],
									'link' => href_link('page', $pages_res['pages_id']),
									'title' => $indent.$pages_res['pages_title']);
									
			pages_list($pages_res['pages_id'], &$catlistids, &$indent);
		}
		return $catlistids;
	}
	$pages = NULL ;
}

Merci par avance pour votre réponse.

pH
 
WRInaute accro
http://www.sitepoint.com/hierarchical-data-database/
PHP:
<span class="syntaxdefault"><br /></span><span class="syntaxkeyword">echo&nbsp;</span><span class="syntaxdefault">str_repeat</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'&nbsp;&nbsp;'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$level</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">$row</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'title'</span><span class="syntaxkeyword">].</span><span class="syntaxstring">"\n"</span><span class="syntaxkeyword">;<br />&nbsp;</span><span class="syntaxdefault"></span>
 
Discussions similaires
Haut