Modified Preorder Traversal Tree (MPTT) - Adding A Child Node

From the first article on MPTT, we know that RGT of Canada is 15. Now if we need to add Alberta as a province of Canada. We will pass the RGT of Canada to the code to insert a new node and shift all nodes whose RGT is greater than or equal to 15.

<?php

/*//////////////////////////GET THE LFT AND RGT VALUES*/

/*////////////////////////// $v HAS BEEN PASSED TO THE FUNCTION*/

$q = "select lft,level from tbllinks where rgt='$V' ";

$r = mysql_query($q);

$row = mysql_fetch_assoc($r);

$parent = $row['lft'];

$rt = $V;

$nr = $rt 1;

/*////////////// THUS WE INCREMENT ALL NODES LFT AND RGT VALUES GREATER THAN PARENT RGT BY 2*/

$q = "update tbllinks set lft =lft 2 where lft >='$V' ";

mysql_query($q);

$q = "update tbllinks set rgt =rgt 2 where rgt >='$V' ";

mysql_query($q);

/*/////////////INSERT THE RECORD NOW*/

$q = "insert into tbllinks values(NULL,'$V','$nr','$parent','###NEWNODE###',)";

mysql_query($q) or die(mysql_error());

?>

 

1-Country-18

2-Canada-17

3-British Columbia-8                               9-Ontario-12        13-Quebec-14        15-Alberta-16

4-Vancouver-5       6-Richmond-7          10-Toronto-11