Codice PHP:
/* getTree
* restituisce l'albero
* relativo ad un nodo passato
* $node è la variabile che indica
* l'id del nodo da scorrere
*/
function getTree($node = 0)
{
$where = "";
// Se viene passato l'id di un nodo
// imposto $where per mostrare solo
// quel nodo
if ($node != 0)
{
// Ottengo i flag dx e sx del nodo
$node = $this->_getProperties($node);
// Costruisco la clausola WHERE
$where = "WHERE " . $this->_prefix . "tree.sx >= '$node[sx]'
AND " . $this->_prefix . "tree.dx <= '$node[dx]'";
} // END if $node != 0
$query = "SELECT " . $this->_prefix . "tree.id AS id,
" . $this->_prefix . "tree.sx AS sx,
" . $this->_prefix . "tree.dx AS dx,
" . $this->_prefix . "details.name AS name
FROM " . $this->_prefix . "tree
LEFT JOIN " . $this->_prefix . "details ON " . $this->_prefix . "details.node = " . $this->_prefix . "tree.id
$where
ORDER BY " . $this->_prefix . "tree.sx ASC";
// Esecuzione della query
// e restituzione del risultato
$result = mysql_query($query);
// Costruisco l'array da esportare
$i = 0; // Contatore dell'indice dell'array
$lastSx = 1; // Ultimo valore del flag sx
$tab = 0; // numero di ripetizioni del carattere di indentazione
while ($row = mysql_fetch_array($result))
{
// Inizio procedimento di indentazione
$sx = (int)$row['sx'];
if ( $sx === ( $lastSx + 1 ) )
{
$tab++;
}
else if ( $sx > ( $lastSx + 2 ) && $tab >= ( $sx - ( $lastSx + 2 ) ) )
{
$tab -= $sx - ( $lastSx + 2 );
}
// Fine procedimento di indentazione
// Assegnazione dei valori all'array da esportare
$export[$i]['id'] = $row['id'];
$export[$i]['name'] = str_repeat( '' , $tab * 5 ) . $row['name'];
$export[$i]['sx'] = $row['sx'];
$export[$i]['dx'] = $row['dx'];
$i++;
$lastSx = $row['sx'];
}
return $export;
} // End getTree()
Eccolo qua, fresco fresco (l'ho fatto circa tre ore fa
)
Se vuoi utilizzare le tabelle della classe di andr3a dovrai rinominare alcune tabelle all'interno del metodo perchè questo è quello che ho creato per la mia classe.
Inoltre dovri cambiare il metodo che ottiene i valori dei flag sx e dx (io utilizzo $this->_getProperties mentre andr3a utilizzava firstQuery() se non sbaglio)
Non dovrebbe essere difficile cmq.
Il metodo accetta opzionalmente l'id di un nodo e ti restituisce l'albero relativo.