|
Posted by Rik on 06/06/07 17:07
On Wed, 06 Jun 2007 15:11:23 +0200, Steve <StevePBurgess@gmail.com> wrot=
e:
> I have been studying the Adjacency List Model as a means of achieving
> a folder structure in a project I am working on. Started with the
> excellent article by Gijs Van Tulder
>
> http://www.sitepoint.com/article/hierarchical-data-database
>
> My database has this basic structure:
>
> Id
> FolderName
> ChildOf
>
> where ChildOf is the Id of the Folder that the current folder is
> nested within.
>
> Although I can use this to make indented text lists - I want the
> output to be a hierarchical HTML structured list using <ol><li>
> structures.
>
> So, for example, my folders are:
>
> News
> -Government Information
> -BBC
> -National Press
> Books
> LIS
> -UKEIG
> -NPRIE
>
> I want the output to be:
>
> <ol>
> <li>News
> <ol>
> <li>Government Information</li>
> <li>BBC></li>
> <li>National Press</li>
> </ol>
> </li>
> <li>Books</li>
> <li>LIS
> <ol>
> <li>UKEIG</li>
> <li>NPRIE</li>
> </ol>
> </li>
> </ol>
>
mysql_query()/mysql_fetch_assoc() etc. could be in the flavour of your =
choice, childof is assumed to be NULL in rootnodes:
function treelist($parent =3D 0,$level =3D 0){
//failsafe:
if($level > 30){
trigger_error('function treelist(): nesting too deep ( > 30), s=
elf =
referencing item?', E_USER_ERROR);
exit;
}
$where =3D ($id =3D=3D 0) ? ' IS NULL ': ' id =3D '.intval($parent)=
;
$res =3D mysql_query('SELECT id,foldername,childof FROM table WHERE=
=
childof '.$where);
if(!mysql_num_rows($res)) return '';
$return =3D "\n".str_repeat(' ',$level).'<ol>';
while($row =3D mysql_fetch_assoc($res)){
$return .=3D "\n".str_repeat(' =
',$level).'<li>'.$row['foldername'].treelist($row['id'],$level+1).'</li>=
';
}
$return .=3D "\n</ol>\n";
if($level > 1) $return .=3D str_repeat(' ',$level-1);
return $return;
}
-- =
Rik Wasmus
[Back to original message]
|