Welcome Guest, Not a member yet? Register   Sign In
need help with datamapper orm and nestedsets
#21

[eluser]kev[/eluser]
I understandly perfectly CroNiX.

Anyway, I think I have been able to figure out. Let me share what I have so far.

Code:
$tree = new Category();
$tree->get_root();

foreach($tree->get_root() as $key => $root)
{
// Using this method, I can return every Tree with its
// corresponding sub items since im passing different root_id
$cats[$key] = $root
       ->select_root($root->root_id)
       ->get_root()
       ->dump_tree( NULL , 'array' , FALSE );
}

// I needed this for loop to avoid multidimensional array
foreach( $cats as $categories)
{
foreach($categories as $key => $category)
{
  $list[] = $category;
}

}

// I use the code below to output UL and LI ..
$result = '';
$prevDepth = null;

foreach ($list as $element) {
if (null === $prevDepth || $element['__level'] > $prevDepth) {
  
  if( $element['__level'] == 0 ) {
   echo "<ul>\n";
  } else {
   echo "\n\t<ul>\n";
  }
  
}
else if ($element['__level'] == $prevDepth) {
  echo "\t</li>";
}
else if ($element['__level'] < $prevDepth) {
  echo str_repeat("</li>\n\t</ul>\n", $prevDepth - $element['__level']) . "\t</li>\n";
}

if( $element['__level'] == 0 ) {
  echo "\t<li>{$element['title']}";
} else {
  echo "\t\t<li>{$element['title']}";
}


$prevDepth = (int) $element['__level'];
}

echo str_repeat("</li>\n</ul>", $prevDepth+1);

The outputted result is

Code:
Tree 1
     Sub Tree 1
Tree 2
     Sub Tree 2
Tree 3

And the XHTML looks like this

Code:
<ul>
<li>Tree 1
  <ul>
   <li>Sub Tree 1</li>
  </ul>
</li>
<li>Tree 2
  <ul>
   <li>Sub Tree 2</li>
  </ul>
</li>
<li>Tree 3</li>
</ul>

I guess that's ok? Do you find any changes that can be done to better it?




Theme © iAndrew 2016 - Forum software by © MyBB