PHP Moving mySQL Tree Node
- by TK
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...