Dopo diversi giorni di ricerche sul web, sono riuscito a trovare un'ottima guida per gestire dati organizzati in struttura gerarchica attraverso Mysql.
http://devzone.zend.com/article/1259...ical-Directory
Tabelle:
CREATE TABLE nodes (
id INT(11) NOT NULL auto_increment,
parent INT,
cat_name VARCHAR(100),
created TIMESTAMP(14),
PRIMARY KEY(id)
);
CREATE TABLE node_data (
PRIMARY KEY(id)
parent INT,
name VARCHAR(100),
id INT(11) NOT NULL auto_increment,
created TIMESTAMP(14),
url VARCHAR(255),
);
Funzione per mostare le categorie:
function showgroup($gnum, $lev) {
$q = mysql_query("SELECT * FROM nodes WHERE parent=$gnum");
while ($q2 = mysql_fetch_array($q)) {
$space = "";
$ttl = "";
for ($i = 0; $i < $lev; $i++) {
$ttl .= $space;
}
echo $ttl."{$q2['cat_name']}";
echo "
\n";
$ttl .= $space . $space;
$links = mysql_query("SELECT url, name FROM node_data WHERE parent={$q2['id']}");
while ($links2 = mysql_fetch_array($links)) {
echo "$ttl{$links2['name']}";
echo "
";
}
showgroup($q2['id'], $lev+1);
}
if (mysql_num_rows($q) <= 0) {
return(0);
}
}
Funziona perfettamente.
Funzione per mostare la categoria dato l'id di un record:
function display_parents($id) {
// $id will be the id of the link we want to display
$link = mysql_query("SELECT parent,name FROM node_data WHERE id=$id");
$link2 = mysql_fetch_array($link);
$parent = $link2[’parent’];
$display = $link2[’name’];
while($parent!=0) {
$dir = mysql_query("SELECT * FROM nodes WHERE $id=$parent");
$dir2 = mysql_fetch_array($dir);
$parent = $dir2[’parent’];
$display = $dir2[’cat_name’].">".$display;
}
}
Non funziona. Non mostra alcun risultato.
Poichè sono ormai ore che mi sbatto su questa funzione, sono al punto che probabilmente c'è qualcosa di davvero sciocco che mi sfugge.
Chi mi può dare una mano?