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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:54:41+00:00 2026-05-14T00:54:41+00:00

I am having trouble trying to move sub nodes or parent nodes up or

  • 0

I am having trouble trying to move sub nodes or parent nodes up or down… not that good at math.

CREATE TABLE IF NOT EXISTS `pages` (   `page-id` mediumint(8) unsigned

NOT NULL AUTO_INCREMENT, page-left
mediumint(8) unsigned NOT NULL,
page-right smallint(8) unsigned NOT
NULL, page-title text NOT NULL,
page-content text NOT NULL,
page-time int(11) unsigned NOT NULL,
page-slug text NOT NULL,
page-template text NOT NULL,
page-parent mediumint(8) unsigned
NOT NULL, page-type text NOT NULL,
PRIMARY KEY (page-id) )
ENGINE=MyISAM DEFAULT CHARSET=latin1
;

INSERT INTO pages (page-id,
page-left, page-right,
page-title, page-content,
page-time, page-slug,
page-template, page-parent,
page-type) VALUES (17, 1, 6, ‘1’,
”, 0, ‘PARENT’, ”, 0, ”), (18, 2,
5, ‘2’, ”, 0, ‘SUB’, ”, 17, ”),
(19, 3, 4, ‘3’, ”, 0, ‘SUB-SUB’, ”,
18, ”), (20, 7, 8, ‘5’, ”, 0,
‘TEST’, ”, 0, ”);

As example how would I move TEST up above PARENT and say move SUB down below SUB-SUB by playing with the page-left/page-right IDs? Code is not required just help with the SQL concept or math for it, would help me understand how to move it better…

  • 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-05-14T00:54:42+00:00Added an answer on May 14, 2026 at 12:54 am

    So basically you want to convert an adjacency list to a nested set? First update your adjacency list (ie. update the page_parent values to the correct values for your new tree), then run the conversion below.

    Using PHP (basic code, untested) :

    class Tree
    {    
        private $count = 0;
        private $data = array();
    
        /**
         * Rebuild nested set
         * 
         * @param $rawData array Raw tree data
         */
        public function rebuild($rawData)
        {
            $this->data = $rawData;
            $this->count = 1;
            $this->traverse(0);        
        }
    
        private function traverse($id)
        {
            $lft = $this->count;
            $this->count++;
    
            if (isset($this->data[$id])) {
                $kid = $this->data[$id];
                if ($kid) {
                    foreach ($kid as $c) {
                        $this->traverse($c);
                    }
                }
            }
    
            $rgt = $this->count;
            $this->count++;
    
            // TODO: Update left and right values to $lft & $rgt in your DB for page_id $id
            ...
        }
    }
    

    When you call this, $rawData should contain an array of ID’s, indexed by parent-id, you could create it (based on your table structure) as follows ($db should contain an active PDO connection object):

        $sql = 'SELECT page_id, page_parent FROM pages ORDER BY page_parent';
    
        $stmt = $db->prepare($sql);
        $rawData = array();
        $stmt->execute();
        while ($row = $stmt->fetch()) {
            $parent = $row['page_parent'];
            $child = $row['page_id'];
            if (!array_key_exists($parent, $rawData)) {
                $rawData[$parent] = array();
            }
            $rawData[$parent][] = $child;
        }
    

    To do the conversion you would need something like :

    $tree = new Tree();
    $tree->rebuild($rawData);
    

    So basically you create an array that is containing all nodes in your tree indexed by parent which will be traversed in a recursive manner to determine the correct left and right values per node.

    BTW You could do it in plain SQL (after you adapt table/column names) :
    http://bytes.com/topic/mysql/answers/638123-regenerate-nested-set-using-parent_id-structure

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

Sidebar

Related Questions

Hey having some trouble trying to maintain transparency on a png when i create
I'm having trouble trying to optimize the following query for sql server 2005. Does
I'm having trouble trying to set the value of a property after i cast
I am having real trouble trying to get decent answers to my questions from
I'm writing some code in python and I'm having trouble when trying to retrieve
I'm having a bit of trouble trying to get class variables to work in
Im trying to extract a line from wget's result but having trouble with it.
I'm trying to build a proxy module for .NET, but I'm having trouble copying
I'm having some trouble installing and configuring qt on my vista laptop. I'm trying
I'm trying to use sed to fix a file, and I'm having trouble grokking

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.