Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- i have write this
- {{{!#php
- <?php
- function display_tree() {
- // retrieve the left and right value of the $root node
- $conn = pg_connect("host=172.16.234.130 port=5432 dbname=db_einv user=dskim1 password=");
- $result = pg_query("
- SELECT node.it_idx,
- node.it_lft,
- node.it_rgt,
- node.it_rgt = node.it_lft + 1 as is_leaf,
- node.it_name, (count(node.it_lft) - 1) as level
- FROM tb_h_item AS node, tb_h_item AS parent
- WHERE node.it_lft BETWEEN parent.it_lft AND parent.it_rgt
- GROUP BY node.it_idx, node.it_lft, node.it_rgt, is_leaf, node.it_name
- ORDER BY node.it_lft");
- // start with an empty $right stack
- $right = array();
- // now, retrieve all descendants of the $root node
- //$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;');
- // display each row
- echo "{";
- while ($row = pg_fetch_array($result)) {
- // only check stack if there is one
- if (count($right)>0) {
- // check if we should remove a node from the stack
- while ($right[count($right)-1]<$row['it_rgt']) {
- array_pop($right);
- }
- }
- // display indented node title
- if ($row['is_leaf']=='t')
- {
- $v ='{"text": "'.$row['it_name'].'",
- "cls": "'.$row['is_leaf'].'",
- "idx": "'.$row['it_idx'].'",
- "lft": "#",
- "rgt": "#"}';
- $b ="";
- }
- else
- {
- $v ='{"text": "'.$row['it_name'].'",
- "cls": "'.$row['is_leaf'].'",
- "idx": "'.$row['it_idx'].'",
- "lft": "##",
- "rgt": "##",
- "children":"["';
- $b = "]";
- }
- //echo str_repeat($v,count($right))."<br />\n";
- echo str_repeat(" ",count($right)*10);
- echo $v."<br />";
- // add this node to the stack
- $right[] = $row['it_rgt'];
- }
- echo "}";
- }
- echo display_tree();
- ?>
- }}}
- and this output like this,
- {{"text": "ROOT", "cls": "f", "idx": "24", "lft": "##", "rgt": "##", "children":"["
- {"text": "ROOT 4 - 3", "cls": "t", "idx": "34", "lft": "#", "rgt": "#"}
- {"text": "ROOT 3", "cls": "f", "idx": "28", "lft": "##", "rgt": "##", "children":"["
- {"text": "ROOT 3 - 3", "cls": "t", "idx": "33", "lft": "#", "rgt": "#"}
- {"text": "ROOT 3 - 2", "cls": "t", "idx": "32", "lft": "#", "rgt": "#"}
- {"text": "ROOT 3 - 1", "cls": "t", "idx": "31", "lft": "#", "rgt": "#"}
- {"text": "ROOT 2", "cls": "t", "idx": "27", "lft": "#", "rgt": "#"}
- {"text": "ROOT 1", "cls": "t", "idx": "26", "lft": "#", "rgt": "#"}
- }
- Masih kurang sempurna :(
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement