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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:07:20+00:00 2026-06-17T15:07:20+00:00

I have implemented jsTree on my site with a php/MySQL back-end for tree storage

  • 0

I have implemented jsTree on my site with a php/MySQL back-end for tree storage and retrieval. I used the php/MySQL demo that came with the jsTree download for the basic infrastructure and then modified to my needs.

I have modified so that multiple trees can be stored in the same database, and added a new column of “owner_id” that stores the userid of the person that created that particular tree.

The php code that creates a new branch or moves a branch is not working correctly as it is not taking into account that there are multiple trees within the database.

jsTree uses the nested set model, and the script is adjusting the left and right values of all the trees in the database instead of just the one that has had a new branch added. This is slowly corrupting the entire database.

The following code shows the function/s that does the adjusting, could someone please try and amend the code for me so it uses the “owner_id” field to only make the changes to a particular tree?

function _create($parent, $position) {
    return $this->_move(0, $parent, $position);
}

and then…

function _move($id, $ref_id, $position = 0, $is_copy = false) {
    $hbhbhbh = fSession::get('nodes_allowed[nodes_access]');
    if ($hbhbhbh == "0" || $hbhbhbh == "2" || $hbhbhbh == "3") {
    if((int)$ref_id === 0 || (int)$id === 1) { return false; }
    $sql        = array();                      // Queries executed at the end
    $node       = $this->_get_node_ifuueuwyhddd($id);       // Node data
    $nchildren  = $this->_get_children($id);    // Node children
    $ref_node   = $this->_get_node_ifuueuwyhddd($ref_id);   // Ref node data
    $rchildren  = $this->_get_children($ref_id);// Ref node children

    $ndif = 2;
    $node_ids = array(-1);
    if($node !== false) {
        $node_ids = array_keys($this->_get_children($id, true));
        // TODO: should be !$is_copy && , but if copied to self - screws some right indexes
        if(in_array($ref_id, $node_ids)) return false;
        $ndif = $node[$this->fields["right"]] - $node[$this->fields["left"]] + 1;
    }
    if($position >= count($rchildren)) {
        $position = count($rchildren);
    }

    // Not creating or copying - old parent is cleaned
    if($node !== false && $is_copy == false) {
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["position"]."` = `".$this->fields["position"]."` - 1 " . 
            "WHERE " . 
                "`".$this->fields["parent_id"]."` = ".$node[$this->fields["parent_id"]]." AND " . 
                "`".$this->fields["position"]."` > ".$node[$this->fields["position"]];
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["left"]."` = `".$this->fields["left"]."` - ".$ndif." " . 
            "WHERE `".$this->fields["left"]."` > ".$node[$this->fields["right"]];
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["right"]."` = `".$this->fields["right"]."` - ".$ndif." " . 
            "WHERE " . 
                "`".$this->fields["right"]."` > ".$node[$this->fields["left"]]." AND " . 
                "`".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ";
    }
    // Preparing new parent
    $sql[] = "" . 
        "UPDATE `".$this->table."` " . 
            "SET `".$this->fields["position"]."` = `".$this->fields["position"]."` + 1 " . 
        "WHERE " . 
            "`".$this->fields["parent_id"]."` = ".$ref_id." AND " . 
            "`".$this->fields["position"]."` >= ".$position." " . 
            ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");

    $ref_ind = $ref_id === 0 ? (int)$rchildren[count($rchildren) - 1][$this->fields["right"]] + 1 : (int)$ref_node[$this->fields["right"]];
    $ref_ind = max($ref_ind, 1);

    $self = ($node !== false && !$is_copy && (int)$node[$this->fields["parent_id"]] == $ref_id && $position > $node[$this->fields["position"]]) ? 1 : 0;
    foreach($rchildren as $k => $v) {
        if($v[$this->fields["position"]] - $self == $position) {
            $ref_ind = (int)$v[$this->fields["left"]];
            break;
        }
    }
    if($node !== false && !$is_copy && $node[$this->fields["left"]] < $ref_ind) {
        $ref_ind -= $ndif;
    }

    $sql[] = "" . 
        "UPDATE `".$this->table."` " . 
            "SET `".$this->fields["left"]."` = `".$this->fields["left"]."` + ".$ndif." " . 
        "WHERE " . 
            "`".$this->fields["left"]."` >= ".$ref_ind." " . 
            ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
    $sql[] = "" . 
        "UPDATE `".$this->table."` " . 
            "SET `".$this->fields["right"]."` = `".$this->fields["right"]."` + ".$ndif." " . 
        "WHERE " . 
            "`".$this->fields["right"]."` >= ".$ref_ind." " . 
            ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");

    $ldif = $ref_id == 0 ? 0 : $ref_node[$this->fields["level"]] + 1;
    $idif = $ref_ind;
    if($node !== false) {
        $ldif = $node[$this->fields["level"]] - ($ref_node[$this->fields["level"]] + 1);
        $idif = $node[$this->fields["left"]] - $ref_ind;
        if($is_copy) {
            $sql[] = "" . 
                "INSERT INTO `".$this->table."` (" .
                    "`".$this->fields["parent_id"]."`, " . 
                    "`".$this->fields["position"]."`, " . 
                    "`".$this->fields["left"]."`, " . 
                    "`".$this->fields["right"]."`, " . 
                    "`".$this->fields["level"]."`" . 
                ") " . 
                    "SELECT " .
                        "".$ref_id.", " . 
                        "`".$this->fields["position"]."`, " . 
                        "`".$this->fields["left"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " . 
                        "`".$this->fields["right"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " . 
                        "`".$this->fields["level"]."` - (".$ldif.") " . 
                    "FROM `".$this->table."` " . 
                    "WHERE " . 
                        "`".$this->fields["id"]."` IN (".implode(",", $node_ids).") " . 
                    "ORDER BY `".$this->fields["level"]."` ASC";
        }
        else {
            $sql[] = "" . 
                "UPDATE `".$this->table."` SET " . 
                    "`".$this->fields["parent_id"]."` = ".$ref_id.", " . 
                    "`".$this->fields["position"]."` = ".$position." " . 
                "WHERE " . 
                    "`".$this->fields["id"]."` = ".$id;
            $sql[] = "" . 
                "UPDATE `".$this->table."` SET " . 
                    "`".$this->fields["left"]."` = `".$this->fields["left"]."` - (".$idif."), " . 
                    "`".$this->fields["right"]."` = `".$this->fields["right"]."` - (".$idif."), " . 
                    "`".$this->fields["level"]."` = `".$this->fields["level"]."` - (".$ldif.") " . 
                "WHERE " . 
                    "`".$this->fields["id"]."` IN (".implode(",", $node_ids).") ";
        }
    }
    else {
        $ewre = fSession::get('user[user_id]');
        $sql[] = "" . 
            "INSERT INTO `".$this->table."` (" .
                "`".$this->fields["owner"]."`, " . 
                "`".$this->fields["parent_id"]."`, " . 
                "`".$this->fields["position"]."`, " . 
                "`".$this->fields["left"]."`, " . 
                "`".$this->fields["right"]."`, " . 
                "`".$this->fields["level"]."` " . 
                ") " . 
            "VALUES (" .
                $ewre.", " .
                $ref_id.", " . 
                $position.", " . 
                $idif.", " . 
                ($idif + 1).", " . 
                $ldif. 
            ")";
    }
    foreach($sql as $q) { $this->db->query($q); }
    $ind = $this->db->insert_id();
    if($is_copy) $this->_fix_copy($ind, $position);
    return $node === false || $is_copy ? $ind : true;
    }
}

Any help really appreciated.

Thanks

  • 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-17T15:07:21+00:00Added an answer on June 17, 2026 at 3:07 pm

    For anyone else this might help, here is my code after the changes to I made to make creating / moving nodes only apply to the particular tree in question.

    Adding the owner_id in the where clause did in fact fix the issue. I needed to add it to only certain queries:

    function _move($id, $ref_id, $position = 0, $is_copy = false) {
        $hbhbhbh = fSession::get('nodes_allowed[nodes_access]');
        if ($hbhbhbh == "0" || $hbhbhbh == "2" || $hbhbhbh == "3") {
        if((int)$ref_id === 0 || (int)$id === 1) { return false; }
        $sql        = array();                      // Queries executed at the end
        $node       = $this->_get_node_ifuueuwyhddd($id);       // Node data
        $nchildren  = $this->_get_children($id);    // Node children
        $ref_node   = $this->_get_node_ifuueuwyhddd($ref_id);   // Ref node data
        $rchildren  = $this->_get_children($ref_id);// Ref node children
    
    
        $ndif = 2;
        $node_ids = array(-1);
    
        if($node !== false) {
    
    
            $node_ids = array_keys($this->_get_children($id, true));
            // TODO: should be !$is_copy && , but if copied to self - screws some right indexes
            if(in_array($ref_id, $node_ids)) return false;
            $ndif = $node[$this->fields["right"]] - $node[$this->fields["left"]] + 1;
        }
    
        if($position >= count($rchildren)) {        
    
            $position = count($rchildren);
        }
    
    
        // Not creating or copying - old parent is cleaned
        if($node !== false && $is_copy == false) {
    
    
            $sql[] = "" . 
                "UPDATE `".$this->table."` " . 
                    "SET `".$this->fields["position"]."` = `".$this->fields["position"]."` - 1 " . 
                "WHERE " .
                    "`".$this->fields["owner"]."` = ".(int) $node[$this->fields["owner"]]." AND " .
                    "`".$this->fields["parent_id"]."` = ".$node[$this->fields["parent_id"]]." AND " . 
                    "`".$this->fields["position"]."` > ".$node[$this->fields["position"]];
            $sql[] = "" . 
                "UPDATE `".$this->table."` " . 
                    "SET `".$this->fields["left"]."` = `".$this->fields["left"]."` - ".$ndif." " . 
                "WHERE `".$this->fields["left"]."` > ".$node[$this->fields["right"]]." AND " .
                    "`".$this->fields["owner"]."` = ".(int) $node[$this->fields["owner"]];
            $sql[] = "" . 
                "UPDATE `".$this->table."` " . 
                    "SET `".$this->fields["right"]."` = `".$this->fields["right"]."` - ".$ndif." " . 
                "WHERE " .
                    "`".$this->fields["owner"]."` = ".(int) $node[$this->fields["owner"]]." AND " .
                    "`".$this->fields["right"]."` > ".$node[$this->fields["left"]]." AND " . 
                    "`".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ";
        }
    
    
    
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["position"]."` = `".$this->fields["position"]."` + 1 " . 
            "WHERE " . 
                "`".$this->fields["parent_id"]."` = ".$ref_id." AND " . 
                "`".$this->fields["position"]."` >= ".$position." " . 
                ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
    
    
    
        $ref_ind = $ref_id === 0 ? (int)$rchildren[count($rchildren) - 1][$this->fields["right"]] + 1 : (int)$ref_node[$this->fields["right"]];
        $ref_ind = max($ref_ind, 1); 
    
    
        $self = ($node !== false && !$is_copy && (int)$node[$this->fields["parent_id"]] == $ref_id && $position > $node[$this->fields["position"]]) ? 1 : 0;
    
        foreach($rchildren as $k => $v) {
    
    
            if($v[$this->fields["position"]] - $self == $position) {
                $ref_ind = (int)$v[$this->fields["left"]];
                break;
            }
        }
    
    
        if($node !== false && !$is_copy && $node[$this->fields["left"]] < $ref_ind) {
    
    
    
            $ref_ind -= $ndif;
        }
    
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["left"]."` = `".$this->fields["left"]."` + ".$ndif." " . 
            "WHERE " . 
                "`".$this->fields["owner"]."` = ".(int) $ref_node[$this->fields["owner"]]." AND `".$this->fields["left"]."` >= ".$ref_ind." " . 
                ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
        $sql[] = "" . 
            "UPDATE `".$this->table."` " . 
                "SET `".$this->fields["right"]."` = `".$this->fields["right"]."` + ".$ndif." " . 
            "WHERE " . 
                "`".$this->fields["owner"]."` = ".(int) $ref_node[$this->fields["owner"]]." AND `".$this->fields["right"]."` >= ".$ref_ind." " . 
                ( $is_copy ? "" : " AND `".$this->fields["id"]."` NOT IN (".implode(",", $node_ids).") ");
    
    
        $ldif = $ref_id == 0 ? 0 : $ref_node[$this->fields["level"]] + 1;
        $idif = $ref_ind;
    
    
        if($node !== false) {
    
    
            $ldif = $node[$this->fields["level"]] - ($ref_node[$this->fields["level"]] + 1);
            $idif = $node[$this->fields["left"]] - $ref_ind;
            if($is_copy) {
    
    
                $sql[] = "" . 
                    "INSERT INTO `".$this->table."` (" .
                        "`".$this->fields["parent_id"]."`, " . 
                        "`".$this->fields["position"]."`, " . 
                        "`".$this->fields["left"]."`, " . 
                        "`".$this->fields["right"]."`, " . 
                        "`".$this->fields["level"]."`" . 
                    ") " . 
                        "SELECT " .
                            "".$ref_id.", " . 
                            "`".$this->fields["position"]."`, " . 
                            "`".$this->fields["left"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " . 
                            "`".$this->fields["right"]."` - (".($idif + ($node[$this->fields["left"]] >= $ref_ind ? $ndif : 0))."), " . 
                            "`".$this->fields["level"]."` - (".$ldif.") " . 
                        "FROM `".$this->table."` " . 
                        "WHERE " . 
                            "`".$this->fields["id"]."` IN (".implode(",", $node_ids).") " . 
                        "ORDER BY `".$this->fields["level"]."` ASC";
            }
            else {
                $sql[] = "" . 
                    "UPDATE `".$this->table."` SET " . 
                        "`".$this->fields["parent_id"]."` = ".$ref_id.", " . 
                        "`".$this->fields["position"]."` = ".$position." " . 
                    "WHERE " . 
                        "`".$this->fields["id"]."` = ".$id;
                $sql[] = "" . 
                    "UPDATE `".$this->table."` SET " . 
                        "`".$this->fields["left"]."` = `".$this->fields["left"]."` - (".$idif."), " . 
                        "`".$this->fields["right"]."` = `".$this->fields["right"]."` - (".$idif."), " . 
                        "`".$this->fields["level"]."` = `".$this->fields["level"]."` - (".$ldif.") " . 
                    "WHERE " . 
                        "`".$this->fields["id"]."` IN (".implode(",", $node_ids).") ";
            }
    
        } else {
    
    
            $ewre = fSession::get('user[user_id]');
            $sql[] = "" . 
                "INSERT INTO `".$this->table."` (" .
                    "`".$this->fields["owner"]."`, " . 
                    "`".$this->fields["parent_id"]."`, " . 
                    "`".$this->fields["position"]."`, " . 
                    "`".$this->fields["left"]."`, " . 
                    "`".$this->fields["right"]."`, " . 
                    "`".$this->fields["level"]."` " . 
                    ") " . 
                "VALUES (" .
                    $ewre.", " .
                    $ref_id.", " . 
                    $position.", " . 
                    $idif.", " . 
                    ($idif + 1).", " . 
                    $ldif. 
                ")";
        }
    
    
        foreach($sql as $q) { $this->db->query($q); }
        $ind = $this->db->insert_id();
        if($is_copy) $this->_fix_copy($ind, $position);
        return $node === false || $is_copy ? $ind : true;
        }
    }
    

    Hope it helps someone…

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

Sidebar

Related Questions

I have implemented Facebook into my app but now I find that whenever I
I have implemented a UISearchDisplayController that allows users to search a table. Currently the
I have implemented a simple entity ejb with a @version annotation. I expect that
Have implemented a site in wordpress with 5 static pages besides the usual blog
I have implemented an eclipse plugin that it launches a java application when the
i have implemented a code that fetch single image from 1000+ images. The image
I have implemented my own RingBuffer , but I'm surprised to see that .NET
I have implemented a custom allocator (to be used by STL containers within my
We have a site that is public facing, let's say it's http://www.example.com . When
I have a custom swing component that is implemented similar to a JTree. It

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.