iceangel89 Posted July 3, 2008 Share Posted July 3, 2008 how can build up something like a site map where 1 site map node can have many child site map nodes and these child can have more children, etc? not actually doing a site map but its similar to it. any suggestions? i am thinking, i need something that i can "add children" so i thought of XML. what do you think? and how should i go about doing this? Edit: forgot to say, my DB is something like - ItemID - ParentID (Referencing ItemID) - Recursive relationship - Item stuff... title etc Link to comment https://forums.phpfreaks.com/topic/113065-how-to-create-a-map-of-items-something-like-sitemap/ Share on other sites More sharing options...
TransmogriBenno Posted July 3, 2008 Share Posted July 3, 2008 You can read the database into a tree and then traverse the tree to output it however you like. e.g. class TreeNode { private $parent; private $children; function __construct () { $this->parent = null; $this->children = array (); } function addChild (TreeNode $child) { $this->children[] = $child; $new_child->setParent ($this); } function setParent ($parent) { if ($parent !== null and !($parent instanceof TreeNode)) throw new Exception ('Invalid parent'); $this->parent = $parent; } } The loading of nodes from the database is the fun part. Basically you loop through your set of rows and attempt to find parents for each node (remove each row from the set as it's added to the tree as a node). Obviously you have to set up a root somewhere. You count the number of children assigned to parents during each iteration of the loop. If it's ever zero, you should break out because there are orphan nodes, meaning your data or algorithm is flawed. The other exit condition would be when the number of nodes that are yet to be assigned to parents is zero. Link to comment https://forums.phpfreaks.com/topic/113065-how-to-create-a-map-of-items-something-like-sitemap/#findComment-580857 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.