Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. i have write this
  2.  
  3.  
  4. {{{!#php
  5. <?php
  6. function display_tree() {
  7. // retrieve the left and right value of the $root node
  8. $conn = pg_connect("host=172.16.234.130 port=5432 dbname=db_einv user=dskim1 password=");
  9.  
  10. $result = pg_query("
  11. SELECT node.it_idx,
  12. node.it_lft,
  13. node.it_rgt,
  14. node.it_rgt = node.it_lft + 1 as is_leaf,
  15. node.it_name, (count(node.it_lft) - 1) as level
  16. FROM tb_h_item AS node, tb_h_item AS parent
  17. WHERE node.it_lft BETWEEN parent.it_lft AND parent.it_rgt
  18. GROUP BY node.it_idx, node.it_lft, node.it_rgt, is_leaf, node.it_name
  19. ORDER BY node.it_lft");
  20. // start with an empty $right stack
  21. $right = array();
  22.  
  23. // now, retrieve all descendants of the $root node
  24. //$result = pg_query('SELECT it_name, it_lft, it_rgt FROM tb_h_item WHERE it_lft BETWEEN '.$row['it_lft'].' AND '$row['it_rgt'].' ORDER BY it_lft ASC;');
  25.  
  26. // display each row
  27. echo "{";
  28. while ($row = pg_fetch_array($result)) {
  29. // only check stack if there is one
  30. if (count($right)>0) {
  31. // check if we should remove a node from the stack
  32. while ($right[count($right)-1]<$row['it_rgt']) {
  33. array_pop($right);
  34. }
  35. }
  36.  
  37. // display indented node title
  38. if ($row['is_leaf']=='t')
  39. {
  40. $v ='{"text": "'.$row['it_name'].'",
  41. "cls": "'.$row['is_leaf'].'",
  42. "idx": "'.$row['it_idx'].'",
  43. "lft": "#",
  44. "rgt": "#"}';
  45. $b ="";
  46. }
  47. else
  48. {
  49. $v ='{"text": "'.$row['it_name'].'",
  50. "cls": "'.$row['is_leaf'].'",
  51. "idx": "'.$row['it_idx'].'",
  52. "lft": "##",
  53. "rgt": "##",
  54. "children":"["';
  55. $b = "]";
  56. }
  57. //echo str_repeat($v,count($right))."<br />\n";
  58. echo str_repeat("&nbsp;",count($right)*10);
  59. echo $v."<br />";
  60. // add this node to the stack
  61.  
  62. $right[] = $row['it_rgt'];
  63. }
  64.  
  65. echo "}";
  66. }
  67. echo display_tree();
  68. ?>
  69.  
  70.  
  71.  
  72. }}}
  73.  
  74. and this output like this,
  75.  
  76. {{"text": "ROOT", "cls": "f", "idx": "24", "lft": "##", "rgt": "##", "children":"["
  77. {"text": "ROOT 4 - 3", "cls": "t", "idx": "34", "lft": "#", "rgt": "#"}
  78. {"text": "ROOT 3", "cls": "f", "idx": "28", "lft": "##", "rgt": "##", "children":"["
  79. {"text": "ROOT 3 - 3", "cls": "t", "idx": "33", "lft": "#", "rgt": "#"}
  80. {"text": "ROOT 3 - 2", "cls": "t", "idx": "32", "lft": "#", "rgt": "#"}
  81. {"text": "ROOT 3 - 1", "cls": "t", "idx": "31", "lft": "#", "rgt": "#"}
  82. {"text": "ROOT 2", "cls": "t", "idx": "27", "lft": "#", "rgt": "#"}
  83. {"text": "ROOT 1", "cls": "t", "idx": "26", "lft": "#", "rgt": "#"}
  84. }
  85.  
  86.  
  87. Masih kurang sempurna :(
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement