Qualche giorno fa ho risolto, se aqualcuno può servire lascio qualche funzioncina che trasforma una base dati come quella indicata Baluba in un array multidimensionale (con l'aggiunta del nome) e poi in un rudimentale elenco puntato
Codice PHP:
function get_root()
{
$query = "SELECT *
FROM catalogo
WHERE id_parent=0";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$array[$row['id']]['name'] = $row['name'];
}
return $array;
}
Codice PHP:
function get_childrens($key,$level)
{
$query = "SELECT *
FROM catalogo
WHERE id_parent=".$key."";
$result = mysql_query($query) or die(mysql_error());
$childrens_db = array();
while($row = mysql_fetch_assoc($result))
{
$childrens_db[] = $row;
}
return $childrens_db;
}
Codice PHP:
function get_catalog(&$data_level,$level)
{
if(count($data_level) > 0)
{
foreach($data_level as $key => $value)
{
if(is_numeric($key))
{
$childrens_db = get_childrens($key,$level);
if(count($childrens_db) > 0)
{
for($i=0; $i<count($childrens_db); $i++)
{
$data_level[$key][$childrens_db[$i]['id']]['name'] = $childrens_db[$i]['name'];
}
}
//echo "<p style=\"text-indent : ".($level * 10)."px\">".$key."</p>";
get_catalog($data_level[$key],$level+1);
}
}
}
return $data_level;
}
Codice PHP:
function catalog_db_to_array()
{
$data_level_root = get_root();
$data_level = get_catalog($data_level_root,1);
return $data_level;
}
Codice PHP:
function array_to_menu($array,&$menu)
{
$menu .= '<ul>'."\n";
foreach($array as $key => $value)
{
if(is_numeric($key))
{
$menu .= '[*][url="?id='.$key.'"]'.$value['name'].'[/url]'."\n";
if(is_array($value) and count($value) > 1)
{
array_to_menu($value,$menu);
}
}
}
$menu .= '[/list]'."\n";
return $menu;
}
un esempio di utilizzo
Codice PHP:
$data_level = catalog_db_to_array();
echo '<pre>'; print_r($data_level); echo '</pre>';
$menu = '';
echo(array_to_menu($data_level,$menu));