Jump to content

How to create a "Map of items" something like sitemap


iceangel89

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.