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 8389839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:49:46+00:00 2026-06-09T18:49:46+00:00

I have the following database: id name unit_id lft rgt level — ————– ——-

  • 0

I have the following database:

id  name            unit_id lft rgt level
--  --------------  ------- --- --- -----
1   Company Name    1       1   2   0
2   Manager         2       2   9   1
3   HR              3       3   8   2
4   Jr.Manager      5       5   8   2
5   Sr.Manager      8       7   8   2

I want to insert record in the above preorder traversal table. For that I used the following code and queries:

function addstructure() {
        $level = $_GET['level'] + 1;
        $rgt = $_GET['rgt'] + 1;
        if ($_GET['level'] == 0) {
            $sql = "UPDATE xp_subunit SET lft = lft+2, rgt=rgt+2 WHERE rgt > " . $_GET['rgt'] . "; ";
            $this->db->query($sql);
            $sql = "INSERT INTO xp_subunit SET level=" . $level . ", lft =" . $_GET['rgt'] . ", rgt =  " . $rgt . " , name = '" . $_GET['name'] . "', unit_id='" . $_GET['unit_id'] . "', description='" . $_GET['description'] . "';";
            $this->db->query($sql);
        } else {
            $sql = "UPDATE xp_subunit SET rgt = rgt+2 WHERE rgt > " . $_GET['lft'] . "; ";
            $this->db->query($sql);
            $sql = "INSERT INTO xp_subunit SET level=" . $level . ", lft =" . $_GET['rgt'] . ", rgt =  " . $rgt . " , name = '" . $_GET['name'] . "', unit_id='" . $_GET['unit_id'] . "', description='" . $_GET['description'] . "';";
            echo $this->db->query($sql);
        }
    }

I am getting lft, rgt and level from database. The problem is when I tried to insert record ex. Jr.Manager and Sr.Manager under Manager category, it is not inserted properly. Instead of “Manager” category its inserting under HR->Jr.Manager->Sr.Manager like.
Whats wrong with above code?

Here is the image for better understanding:

Here is the image for better understanding:

  • 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-09T18:49:47+00:00Added an answer on June 9, 2026 at 6:49 pm

    I am slightly modified your code just try this:

        $name = $_GET['name'];
        $rgt = $_GET['rgt'];
        $lft = $_GET['lft'];
        $level = $_GET['level'] + 1;
        $right = $rgt + 1;
    
        $sql1 = "UPDATE xp_subunit SET lft = lft+2 WHERE lft >= $rgt ;";
        $this->db->query($sql1);
        $sql2 = "UPDATE xp_subunit SET rgt = rgt+2 WHERE rgt >= $rgt ;";
        $this->db->query($sql2);
        $sql3 = "INSERT INTO xp_subunit (`id`,`name`,`unit_id`,`lft`, `rgt`, `level`)        
    values (NULL, '$name', '$unit_id',$rgt, $right, '$level');";
        echo $this->db->query($sql3);
    

    It will work for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following entry in my table: id name unit_id lft rgt level
I have the following database structure. ID | Name | Marks I am trying
I have the following database design: Employee Table: Username, Name, DivisionCode Division Table: SapCode,
I have the following database design: Employee Table: Username, Name Quiz Table: QuizID, Title,
I have the following (simplified) database tables: Products - ProductID, int, PK - Name,
In my mysql database I have the following information in a page name field.
I have the following two tables in my mysql database. Table Name: groups id
I have the following database schema : Store StoreId Name ... Order OrderId StoreId
I have the following database structure: Event table Id - Guid (PK) Name -
I have the following database structure: Sites table id | name | other_fields Backups

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.