Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:18:41+00:00 2026-06-09T00:18:41+00:00

Based on : Getting a modified preorder tree traversal model (nested set) into a

  • 0

Based on : Getting a modified preorder tree traversal model (nested set) into a <ul>

I have following table :

id  name                            lft rgt level
1   company                         1   22  0
75  Developer                       26  31  1
76  Tester                          24  27  1
77  Analyst                         22  23  1
78  under developer                 27  30  2
79  under tster                     25  26  2

And following query/code for fetching nested records:

    function getstructureInformation() {
        $treeArr = array();
        $tree = array();
        $sql = "SELECT node.name, node.id, node.unit_id,
               node.description,node.lft,node.rgt,node.level, 
               (COUNT(parent.name) - 1) AS depth
               FROM tablename AS node
               CROSS JOIN tablename AS parent
               WHERE node.lft BETWEEN parent.lft AND parent.rgt
               GROUP BY node.name
               ORDER BY node.lft";

        $query = $this->db->query($sql);
        $data = $query->result();

        foreach ($data as $datap) {
            $treeArr['id'] = $datap->id;
            $treeArr['unit_id'] = $datap->unit_id;
            $treeArr['lft'] = $datap->lft;
            $treeArr['rgt'] = $datap->rgt;
            $treeArr['level'] = $datap->level;
            $treeArr['name'] = $datap->name;
            $treeArr['depth'] = $datap->depth;
            $treeArr['description'] = $datap->description;
            $tree[] = $treeArr;
        }
        return $tree;
    }

Here the PHP code for displaying it into the view page:

$result = '';
 $currDepth = -1;  // -1 to get the outer <ul>
 while (!empty($tree)) {
  $currNode = array_shift($tree);
  if ($currNode['depth'] > $currDepth) {
    echo '<ul>';
  }
  if ($currNode['depth'] < $currDepth) {       
    $result .= str_repeat('</ul>', $currDepth - $currNode['depth']);
  }
  echo '<li>' . $currNode['name'] . '</li>';
  $currDepth = $currNode['depth'];
  if (empty($tree)) {
    echo str_repeat('</ul>', $currDepth + 1);
  }
}

But its not displaying properly like:

It displaying output as:

001 : Company Name

    3 : Analyst      

2 : Tester      

    33 : under tster      
        1 : Developer      
        44 : under developer

The desired output is like:

001 : Company Name

    3 : Analyst  
    2 : Tester  
        33 : under tster  
    1 : Developer  
        44 : under developer

Is there any solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:18:45+00:00Added an answer on June 9, 2026 at 12:18 am

    This should work :

    function printTree ($tree) {
        $last_level = -1;
    
        foreach ($tree as $v) {
            $diff = $v['level'] - $last_level;
            if ($diff == 0)  {
                echo '<li>' .$v['name']. '</li>';
            }
            elseif ($diff > 0) {
                for ($i = 0; $i < $diff; $i++)
                    echo '<ul>';
                echo '<li>' .$v['name']. '</li>' ;
            }
            else {
                for ($i = 0; $i > $diff; $i--)
                    echo '</ul>';
                echo '<li>' .$v['name']. '</li>' ;
            }
            $last_level = $v['level'];
        }
    }
    

    But with this function, you won’t have the depth printed.

    Tested and working with

    $tree = array (
        array (
            'name' => 'Line',
            'level' => 0
        ),
        array (
            'name' => 'Line',
            'level' => 1
        ),
        array (
            'name' => 'Line',
            'level' => 2
        ),
        array (
            'name' => 'Line',
            'level' => 1
        )
    );
    printTree ($tree);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on this question: Getting a modified preorder tree traversal model (nested set) into
Based on Getting a modified preorder tree traversal model (nested set) into a <ul>
I have a problem getting the right Price for a product based on Effectivity
I have a Mediacore-based website that is getting stuck due to an update query.
after some advice regarding a problem i am getting using a linux based piece
Based on an answer from a candidate I have a confusion regarding the functioning
I have modified my application, so that when a new user is registered, it's
I'm attempting to build a n-gram language model based on the top 100K words
I have been wrestling with getting databinding to work in WPF for a little
i like the .NET MVC practice of getting an instantiated class model passed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.