E quale sarebbe la migliore? Io in un esempio in rete ho visto che veniva utilizzato questo codice:
codice:
// $node is the name of the node we want the path of
function get_path($node) {
// look up the parent of this node
$result = mysql_query('SELECT parent FROM tree '.
'WHERE title="'.$node.'";');
$row = mysql_fetch_array($result);
// save the path in this array
$path = array();
// only continue if this $node isn't the root node
// (that's the node with no parent)
if ($row['parent']!='') {
// the last part of the path to $node, is the name
// of the parent of $node
$path[] = $row['parent'];
// we should add the path to the parent of this node
// to the path
$path = array_merge(get_path($row['parent']), $path);
}
// return the path
return $path;
}
La ricorsione l'ho imparata leggendo proprio questo articolo. Il problema è che utilizzando la funzione proposta l'output è "Array" e non ho capito perchè. Ho provato anche ricreando la tabella dell'esempio ma niente lo stesso. Ora vi posto pure il codice dell'esempio della tabella così se qualcuno vuole provare a trovare l'errore...
codice:
#
# Struttura della tabella `prova`
#
CREATE TABLE `prova` (
`parent` varchar(255) default NULL,
`title` varchar(255) default NULL
) TYPE=MyISAM;
#
# Dump dei dati per la tabella `prova`
#
INSERT INTO `prova` VALUES (NULL, 'food');
INSERT INTO `prova` VALUES ('food', 'fruit');
INSERT INTO `prova` VALUES ('fruit', 'green');
INSERT INTO `prova` VALUES ('green', 'pear');
INSERT INTO `prova` VALUES ('fruit', 'red');
INSERT INTO `prova` VALUES ('red', 'cherry');
INSERT INTO `prova` VALUES ('fruit', 'yellow');
INSERT INTO `prova` VALUES ('yellow', 'banana');
INSERT INTO `prova` VALUES ('food', 'meat');
INSERT INTO `prova` VALUES ('meat', 'beef');
INSERT INTO `prova` VALUES ('meat', 'pork');
Io onestamente non sono riuscito a trovare l'errore. Spero che qualcuno mi possa aiutare! Grazie ancora!