By: RwD
Date: 2005-02-24
Time: 15:59
|
Tree structureI am trying to build up a tree from a database structure but for one reason or the othe I cannot see how to do it for an unknown number of subtrees
First the db structure of the tree. Basically it is a celko nested set but I maintain the parent-child structure for security. (http://www.sitepoint.com/article/hierarchical-data-database/2 to know more about the nested set structure) This is the tree in the database (I made a tree in the name column for easy reading)
Now I would like to display this tree as follows:
As you can see it sounds all very simple but for some reason or the other tbs does not want me to make lists as deep as the data happens to indicate instead I seem to have to make up a depth and hope the actual data does not exceed this... Does anybody know of a generic way to get the specified output using tbs and the shown data structure? I myself have been trying along the way of a template including itself: ($tmpl['menu'] = 'menu.tpl';)
|
|||
By: RwD
Date: 2005-02-24
Time: 16:44
|
Re: Tree structureTo make it easier I wrote a script that runs on a resultset being retrieved in the order I posted before. It then only uses the name and level elements (retrieval was one query sorted on the "left" value where "left > startnode.left" and "right < startnode.right" (If you don't understand then read the link I posted before)).
|
|||
By: Skrol29
Date: 2005-02-24
Time: 16:48
|
Re: Tree structureHi RwD,
This is not obvious because automatic TBS special blocks are only headergrp, footergrp and splitgrp which are activated when a value changes. But in the cas of hierachical representation, value changing is not enougth, it matters also if it's growing or decreasing. If it is ok for you to close <li> tags just after the item, instead of after the children items, the I can propose a solition. Here is the target result :
Then you can code:
Html:
|
|||
By: RwD
Date: 2005-02-24
Time: 16:52
|
Re: Tree structureNo, it is not acceptable to have an ul inside another ul. This is not valid HTML4.01 strict which I am affraid I have to code. The ul element can appear within the li element, so that's where it needs to be.
Perhaps this is something to build into a later version of tbs? Because if tbs cannot do it I will have to go with the function I posted here :( bummer :P |
|||
By: Skrol29
Date: 2005-02-24
Time: 17:21
|
Re: Tree structureOk, that's true.
So all becomes more complicated, but here is the solution. HTML:
PHP:
|
|||
By: RwD
Date: 2005-02-24
Time: 20:28
|
Re: Tree structureIs there perhaps a local/global problem?
I get this error after removing the parsing errors I got TinyButStrong Error (Array value): Can't merge [blk.open] because there is no key named 'open'. This message can be cancelled using parameter 'noerr'. TinyButStrong Error (Array value): Can't merge [blk.close] because there is no key named 'close'. This message can be cancelled using parameter 'noerr'. For as far as I can see the CurrRec variable is a refference to the actual variable so it should be allowed to be changed. Got me puzzeld |
|||
By: RwD
Date: 2005-02-24
Time: 20:31
|
Re: Tree structureUpdate :P
I put echo "hi"; in the onformat function and it only gets called by the ExtraRec part :S |
|||
By: RwD
Date: 2005-02-24
Time: 20:32
|
Re: Tree structureUpdate 2 :P
I forgot to mention that it does output the node names all below eachother |
|||
By: RwD
Date: 2005-02-24
Time: 21:47
|
Re: Tree structureI was looking at the onformat function, and it isn't as it should be. So looking it up I think onformat should have been onsection ;)
In any case, it is functioning but not working... This is the output at home (different structure) (I added the level - to the name):
It should have been
If you know the solution you are welcome to tell me. But now I have a functioning piece of code I will probably make it. But I am wondering what the big advantage of this system is over the function I posted above. the html is in the php script!!!! |
|||
By: Pirjo Posio
Date: 2005-02-24
Time: 22:09
|
Re: Tree structureCan't say if it's best to use your own php-script or tbs.
I was just wondering whether it would be ok to use only <div> and no <ul><li>-pairs, like this (part of php-file, not tbs) '<div style="margin-left: ' . $item['level'] . 'em;">' . $item['node_name'] . '</div>' Maybe this is easier to modify into a tbs-template? |
|||
By: RwD
Date: 2005-02-24
Time: 22:18
|
Re: Tree structureThanks for the suggestion.
It might be easier, however, I am building websites using meaningfull tags and no inline styles. so menu's I only build with lists or definition lists (dl,dt and dd) I did not discuss any styling but you offer styling as the solution :( |
|||
By: RwD
Date: 2005-02-24
Time: 22:55
|
Re: Tree structureSorry for the message spree :P
But I solved the puzzle by just copying my own code into the function. the following was changed:
But still I wonder how this way is any better from my function. Do you think we just have to manage with this way, or will a structural solution be added in the future :o) (I have no idea if it is easy to create; this does imply recursion I think (unlike the solution we both came to now)) Anyway, thanks for the help :) |
|||
By: RwD
Date: 2005-02-25
Time: 09:45
|
Re: Tree structureTrying to be complete I will now post the solution as it is working now. It is different from the code skrol29 posted and some variable names should be changed I think. But for future reference this will do:
PHP:
HTML:
This code will work if your database structure is as I described before. I am using tbs 2.01. I did alter the code before posting it (fieldnames that are different in my db structure, and the query I use is different) but everything should work. If it doesn't I do not suppose it is a lot of work to make it work ;) |
|||
By: Youri
Date: 2007-05-11
Time: 18:49
|
Re: Tree structureThis is great piece of code. Makes perfectly nested ul-li menus.
But what I don't understand is: When I have new line in open string like this:
It outputs new line in final html code. But if I put the same to str_repeat construction:
I get
|
|||
By: aphexx
Date: 2007-05-15
Time: 12:47
|
Re: Tree structure"<ul>\n<li>"
or '</ul>\n</li>' see the difference? single quotes outputs everything as it is written and a newline is not actually \n but a special character so it needs to be parsed. so in short: replace the single quotes with double quotes and it should work |
|||
By: andy
Date: 2007-10-23
Time: 23:25
|
Re: Tree structureI used the last code that RwD posted. My table is the same structure as the one RwD posted on the first post, but I got error like this:
TinyButStrong Error in field [blk.open...] : item 'open' is not an existing key in the array. This message can be cancelled using parameter 'noerr'. TinyButStrong Error in field [blk.open...] : item 'open' is not an existing key in the array. This message can be cancelled using parameter 'noerr'. TinyButStrong Error in field [blk.open...] : item 'open' is not an existing key in the array. This message can be cancelled using parameter 'noerr'. TinyButStrong Error in field [blk.open...] : item 'open' is not an existing key in the array. This message can be cancelled using parameter 'noerr'. Anyone has this problem? Thanks |
|||
By: andy
Date: 2007-10-23
Time: 23:54
|
Re: Tree structureI tried to test it out, but it didn't work, here's url
http://freeeh.com/tree4/tree.php also, this is the code: PHP
HTML is inside a table
|
|||
By: Pirjo Posio
Date: 2007-10-24
Time: 02:19
|
Re: Tree structureHi andy,
The only error that I can see in your code is that TBS no longer has 'onsection' functions. Now we use 'ondata' instead. I tested this code, but had to use an array as data for the test. I also used <div> instead of a table to wrap the <ul><li>-list. Here's my code.
|
|||
By: Andy
Date: 2007-10-24
Time: 03:26
|
Re: Tree structureondata makes it work right away. Thanks
|
|||
By: Pirjo Posio
Date: 2007-10-24
Time: 03:52
|
Re: Tree structureI want to resend correct version of my posting, to get the levels shown (and links):
|
|||
By: TomH
Date: 2008-01-30
Time: 14:13
|
Re: Tree structureHi to all tree structure weird ones,
I was working on hierarchy menu tree and found both the Modified Preordered Tree Traversal by Joe Celko and the parentage/lineage approach. I chose to attempt the parentage/lineage approach and have added it to the "Tips and Tricks" part of the forum. -- your comments would be appreciated -- especially comparing the ease of maintenance and rebuilding the lineage value for each item. The actual demo pages and source code can be found at http://tomhenry.us/tbs3/ Any ideas you care to contribute to improving my lineage approach, would appreciate posting them on the "Tips and Tricks" thread. Thanks for TBS, TomH |