473,508 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adjacency List Model Hierarchical Structure into HTML Structured List

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/hie...-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>

and for this to work irrespective of the number of levels in the
hierarchy.

Any ideas?

Jun 6 '07 #1
12 5789

"Steve" <St***********@gmail.comschreef in bericht
news:11**********************@p77g2000hsh.googlegr oups.com...
>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/hie...-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>

and for this to work irrespective of the number of levels in the
hierarchy.

Any ideas?
Recursion my friend. I've done something simular recently, but a bit
verbose; in every recursion I used an SQL query.

However, yesterday I stumbled across an example that does the job far more
efficient:

http://www.tagarga.com/blok/on/061029

Courtesy of newsgroup contributor gosha bine.

HTH
Jun 6 '07 #2
Rik
On Wed, 06 Jun 2007 15:11:23 +0200, Steve <St***********@gmail.comwrote:
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/hie...-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 = 0,$level = 0){
//failsafe:
if($level 30){
trigger_error('function treelist(): nesting too deep ( 30), self
referencing item?', E_USER_ERROR);
exit;
}
$where = ($id == 0) ? ' IS NULL ': ' id = '.intval($parent);
$res = mysql_query('SELECT id,foldername,childof FROM table WHERE
childof '.$where);
if(!mysql_num_rows($res)) return '';
$return = "\n".str_repeat(' ',$level).'<ol>';
while($row = mysql_fetch_assoc($res)){
$return .= "\n".str_repeat('
',$level).'<li>'.$row['foldername'].treelist($row['id'],$level+1).'</li>';
}
$return .= "\n</ol>\n";
if($level 1) $return .= str_repeat(' ',$level-1);
return $return;
}
--
Rik Wasmus
Jun 6 '07 #3

"amygdala" <no*****@noreply.comschreef in bericht
news:46***********************@news.kpnplanet.nl.. .
>
"Steve" <St***********@gmail.comschreef in bericht
news:11**********************@p77g2000hsh.googlegr oups.com...
>>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/hie...-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>

and for this to work irrespective of the number of levels in the
hierarchy.

Any ideas?

Recursion my friend. I've done something simular recently, but a bit
verbose; in every recursion I used an SQL query.

However, yesterday I stumbled across an example that does the job far more
efficient:

http://www.tagarga.com/blok/on/061029

Courtesy of newsgroup contributor gosha bine.

HTH
Forget what I said, although useful, not exactly what you asked for of
course. Rik's example was more on point.
Jun 6 '07 #4
On 6 Jun, 18:07, Rik <luiheidsgoe...@hotmail.comwrote:
>
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 = 0,$level = 0){
//failsafe:
if($level 30){
trigger_error('function treelist(): nesting too deep ( 30), self
referencing item?', E_USER_ERROR);
exit;
}
$where = ($id == 0) ? ' IS NULL ': ' id = '.intval($parent);
$res = mysql_query('SELECT id,foldername,childof FROM table WHERE
childof '.$where);
if(!mysql_num_rows($res)) return '';
$return = "\n".str_repeat(' ',$level).'<ol>';
while($row = mysql_fetch_assoc($res)){
$return .= "\n".str_repeat('
',$level).'<li>'.$row['foldername'].treelist($row['id'],$level+1).'</li>';
}
$return .= "\n</ol>\n";
if($level 1) $return .= str_repeat(' ',$level-1);
return $return;}
Hi thanks for this. I don't seem to be able to make it work. I have
made the root nodes in the database have a NULL ChildOf but each time
I get the "nesting too deep ( 30), self referencing item" error. Hi
have checked and double checked - and there are no self-referencing
items.

I've tried to debug by checked what $where contains and on each
iteration it just contains IS NULL. What do the ($id==0) and the
'id=" . intval($parent) parts do? There doesn't seem to be an $id
variable declared or set anywhere in the php. I tried changing the
$id==0 to $parent==0 and this produces only the top level list. If I
specify a different "level" this only has the effect of inserting gaps
between each item but it still only returns the top level items.

What I am hoping to achieve is the ability to display the fully
expanded hierarchy.

Any further thoughts???

Best regards, and many thanks,

Steve

Jun 6 '07 #5
On 6 Jun, 17:54, "amygdala" <nore...@noreply.comwrote:
Recursion my friend. I've done something simular recently, but a bit
verbose; in every recursion I used an SQL query.

However, yesterday I stumbled across an example that does the job far more
efficient:

http://www.tagarga.com/blok/on/061029
Hi there. Thanks for the suggestion - but I am already able to produce
the fully expanded and indented list. What I want to do is convert
this into a HTML compliant hierarchical list using nested <oland
<litags.

Best regards
Steve

Jun 6 '07 #6
Rik
On Wed, 06 Jun 2007 19:07:30 +0200, Rik <lu************@hotmail.comwrote:
$where = ($id == 0) ? ' IS NULL ': ' id = '.intval($parent);
Obviously:
$where = ($id == 0) ? ' IS NULL ': ' = '.intval($parent);
--
Rik Wasmus
Jun 6 '07 #7
Rik
On Wed, 06 Jun 2007 19:47:42 +0200, Rik <lu************@hotmail.comwrote:
On Wed, 06 Jun 2007 19:07:30 +0200, Rik <lu************@hotmail.com
wrote:
> $where = ($id == 0) ? ' IS NULL ': ' id = '.intval($parent);

Obviously:
$where = ($id == 0) ? ' IS NULL ': ' = '.intval($parent);
DOH!!!!
Obviously:
$where = ($parent == 0) ? ' IS NULL ': ' = '.intval($parent);

And use it without arguments to get the tree from the root:

treelist();

Only give arguments (the first being the id which you'd like to expand) if
you're only interested in a sub-tree.
--
Rik Wasmus
Jun 6 '07 #8
Rik
On Wed, 06 Jun 2007 19:37:41 +0200, Steve <St***********@gmail.comwrote:
I've tried to debug by checked what $where contains and on each
iteration it just contains IS NULL.
Yup, little bug in the code (haven't tested it, almost never do for
newsposts :P ), should be solved now.
--
Rik Wasmus
Jun 6 '07 #9
On 6 Jun, 18:54, Rik <luiheidsgoe...@hotmail.comwrote:
Yup, little bug in the code (haven't tested it, almost never do for
newsposts :P ), should be solved now.
--
Rik Wasmus
Hey thanks very much. Works a treat!!!

You're a star.

Steve

Jun 6 '07 #10
On 06.06.2007 15:11 Steve wrote:
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/hie...-data-database

[snip]
Any ideas?
Doesn't this article also explain why you should NOT use adjacency
lists? With recursive queries you're doing N selects to fetch N
elements, that makes using database essentially pointless. From my
experience, ALs are only suitable for very small sets, which can be read
once completely and then processed in memory.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 6 '07 #11
On 6 Jun, 19:16, gosha bine <stereof...@gmail.comwrote:
Doesn't this article also explain why you should NOT use adjacency
lists? With recursive queries you're doing N selects to fetch N
elements, that makes using database essentially pointless. From my
experience, ALs are only suitable for very small sets, which can be read
once completely and then processed in memory.
Hi there. Yes it does - and recommends the "Modified Preorder Tree
Traversal" method.

I couldn't get this to work when the tree has several starting nodes
as mine does.

Would welcome any hints about finding a solution to this and producing
a HTML structured list from the results.

Best regards.
Steve

Jun 6 '07 #12
Rik
On Wed, 06 Jun 2007 20:59:03 +0200, Steve <St***********@gmail.comwrote:
On 6 Jun, 19:16, gosha bine <stereof...@gmail.comwrote:
>Doesn't this article also explain why you should NOT use adjacency
lists? With recursive queries you're doing N selects to fetch N
elements, that makes using database essentially pointless. From my
experience, ALs are only suitable for very small sets, which can be read
once completely and then processed in memory.
Well, updating serveral hundreds of thousands of left & right values also
puts some strain on the database. I would not like to use a Nested Set for
a regularly changing gigantic tree.
Hi there. Yes it does - and recommends the "Modified Preorder Tree
Traversal" method.

I couldn't get this to work when the tree has several starting nodes
as mine does.
What problem are you facing then? Several startnodes are absolutely no
problem at all....
Would welcome any hints about finding a solution to this and producing
a HTML structured list from the results.
It might look something like this, not thoroughly tested though...

function treelist_nested_set($id = 0,$limit = -1){
$strLimit = ($limit -1) ? " HAVING depth <= ".intval($limit): '';
if($id==0){
$qry = "SELECT node.id,node.name, (COUNT(parent.name) - 1) AS depth
FROM `table` AS node,
`table` AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.id
{$strLimit}
ORDER BY node.lft";
} else {
$qry = "SELECT node.id,node.name, (COUNT(parent.name) -
(sub_tree.depth + 1)) AS depth
FROM `table` AS node,
`table` AS parent,
`table` AS sub_parent,
(
SELECT node.id,node.name, (COUNT(parent.name) - 1) AS depth
FROM `table` AS node,
`table` AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.id = {$id}
GROUP BY node.id
ORDER BY node.lft
)AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
AND sub_parent.id = sub_tree.id
GROUP BY node.id
{$strLimit}
ORDER BY node.lft";
}
$res = mysql_query($qry);
if(!mysql_num_rows($res))
trigger_error('Could not build tree');
return '';
}
$depth = -1;
$return = '';
while($row = mysql_fetch_assoc($res)){
if($row['depth'] < $depth) $return .=
str_repeat("\n</ol>\n</li>",$depth - $row['depth']);
if($row['depth'] $depth) $return .=
str_repeat("\n<ol>\n",$row['depth'] - $depth);
if($row['depth'] == $depth) $return .= '</li>';
$depth = $row['depth'];
$return .= "\n<li>".$row['name'];
}
$return .= str_repeat("\n</ol>\n</li>",$depth)."\n</ol>";
return $return;
}
--
Rik Wasmus
Jun 6 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4102
by: Mike N. | last post by:
Hello to all: First let me apologize for the length of this question, I've made an attempt to include as much information as is needed to help with the question. I am having problems putting...
0
1777
by: Mike Tyndall | last post by:
Whoops, way late this time. Sorry again, things have been hectic. Updates: -Books added to the "Other C++ books" section: Inside the C++ Object Model -Books added to the "General programming"...
0
7051
by: Jef Driesen | last post by:
I need some help to implement the adjacency list representation of a (undirected) graph. The data structure I need is something like the picture on the website...
8
4318
by: tom | last post by:
I am new to SQL administration. >From a list of IDs that are the primary key in one table (i.e. Customer Table), I want to make changes in tables that use those IDs as a foreign key. ...
5
2236
by: clintonG | last post by:
I'm looking for documentation and would not turn my nose up to any code from anybody who thinks they are good at the design of an algorythm that can be used to generated a hierarchical relational...
5
2116
by: Kevin C | last post by:
I was curious to know what some developers out in the industry are doing when it comes to exposing Data access logic, specifically persistence. This is assuming that your not using an O/R framework...
4
1186
by: Mike J | last post by:
You can find the Tablane extensions for RSS(draft) at: http://www.tablane.net/schema/tblspec.htm The hierarchical structured information can be carried via RSS feed. We'd like to hear comment....
1
2408
by: Aad vd Naad | last post by:
Hi, I have create a dropdown menu which looks as expected in Safari (Mac). Viewing it in FF (Mac) or IE6 (Windows) it's a bit messed up. It also seems that when the top level links are clicked...
0
1135
by: amodagni | last post by:
Hi ! Could someone please guide me to SQL code required for managing data in tables in which hierarchical data stored in adjacency or Nested set model.
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.