Aiuto ! Sono incasinato ma e' un problema carino. La soluzione meno onerosa dal punto di vista computazionale mi sembra la migliore anche se la piu' complicata da implementare.
Questo codice funziona ma non riesco a scrivere i tag UL e LI.
Codice PHP:
<?php
function display_tree($root,$db) {
// retrieve the left and right value of the $root node
$result = mysql_query('SELECT lft, rgt FROM rsscategories '. 'WHERE name="'.$root.'";',$db);
$row = mysql_fetch_array($result);
// start with an empty $right stack
$right = array();
// now, retrieve all descendants of the $root node
$result = mysql_query('SELECT * FROM rsscategories WHERE lft BETWEEN '.$row['lft'].' AND '. $row['rgt'].' ORDER BY lft ASC;',$db);
// display each row
while ($row = mysql_fetch_array($result)) {
// only check stack if there is one
if (count($right)>0) {
// check if we should remove a node from the stack
while ($right[count($right)-1]<$row['rgt']) {
array_pop($right);
}
}
// display indented node title
echo str_repeat('',count($right)).$row['name']."
";
// add this node to the stack
$right[] = $row['rgt'];
}
}
?>
<?
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "t3";
$db = mysql_connect($db_host, $db_user, $db_password);
if ($db == FALSE)
die ("Errore nella connessione.");
mysql_select_db($db_name, $db)
or die ("Errore nella selezione del database.");
display_tree('Massimiliano',$db);
?>
La tabella e' questa:
id name lft rgt
1 Massimiliano 1 14
2 Francesco 2 13
3 Mario 3 4
4 Luigi 5 6
5 Fabio 7 12
6 Piero 8 9
7 Gianni 10 11