/[cvs]/nfo/php/libs/net.php.pear/Tree/docs/Memory_DBnested.php
ViewVC logotype

Contents of /nfo/php/libs/net.php.pear/Tree/docs/Memory_DBnested.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Thu Feb 27 16:53:26 2003 UTC (21 years, 6 months ago) by joko
Branch: MAIN
+ added doc/, from PEAR

1 <?php
2 //
3 // Id: Memory_DBnested.php,v 1.2 2003/01/30 17:43:41 cain Exp
4 // $Id: Memory_DBnested.php,v 1.2 2003/01/30 17:43:41 cain Exp $
5 //
6
7 //ini_set('include_path',realpath(dirname(__FILE__).'/../../').':'.realpath(dirname(__FILE__).'/../../../includes').':'.ini_get('include_path'));
8 //ini_set('error_reporting',E_ALL);
9
10 /**
11 * this is a helper function, so i dont have to write so many prints :-)
12 * @param array $para the result returned by some method, that will be dumped
13 * @param string $string the explaining string
14 */
15 function dumpHelper( $para , $string='' , $addArray=false )
16 {
17 global $tree,$element;
18
19 if( $addArray )
20 eval( "\$res=array(".$para.');' );
21 else
22 eval( "\$res=".$para.';' );
23
24 print '<b>'.$para.' </b><i><u><font color="#008000">'.$string.'</font></u></i><br>';
25 // this method dumps to the screen, since print_r or var_dump dont
26 // work too good here, because the inner array is recursive
27 // well, it looks ugly but one can see what is meant :-)
28 $tree->varDump($res);
29 print '<br>';
30
31 }
32
33 /**
34 * dumps the entire structure nicely
35 * @param string $string the explaining string
36 */
37 function dumpAllNicely( $string='' )
38 {
39 global $tree;
40
41 print '<i><u><font color="#008000">'.$string.'</font></u></i><br>';
42 $all = $tree->getNode(); // get the entire structure sorted as the tree is, so we can simply foreach through it and show it
43 foreach( $all as $aElement )
44 {
45 for( $i=0 ; $i<$aElement['level'] ; $i++)
46 print '&nbsp; &nbsp; ';
47 print '<font color="red">'.$aElement['name'].'</font> ===&gt; ';
48 $tree->varDump(array($aElement));
49 }
50 print '<br>';
51
52 }
53
54
55 /*
56
57 use this to build the db table
58
59 CREATE TABLE test_tree (
60 id int(11) NOT NULL auto_increment,
61 parentId int(11) NOT NULL default '0',
62 name varchar(255) NOT NULL default '',
63 PRIMARY KEY (id)
64 )
65
66
67 This example demonstrates how to manage trees
68 that are saved in a DB, it uses a very simple
69 DB-structure, not nested trees (ok, that sucks, but it can be implemented :-) )
70
71 it reads out the entire tree upon calling the method
72 'setup', then you can work on the tree in whichever way
73 you want, just have a look at the examples
74 there are different ways to achieve things,
75 i will try to demonstrate (all of) them
76
77 */
78
79 require_once('Tree/Tree.php');
80
81 // define the DB-table where the data shall be read from
82 $options = array( 'table' => 'MemoryNestedTree'
83 ,'whereAddOn'=>"comment=''"
84 );
85
86 // calling 'setupMemory' means to retreive a class, which works on trees,
87 // that are temporarily stored in the memory, in an array
88 // this means the entire tree is available at all time !!!
89 // consider the resource usage and it's not to suggested to work
90 // on huge trees (upto 1000 elements it should be ok, depending on your environment and requirements)
91
92 // using 'setupMemory'
93 $tree = Tree::setupMemory( 'DBnested', // use the nested DB schema, which is actually implemented in Dynamic/DBnested
94 // the class Memory/DBnested is only kind of a wrapper to read the entire tree
95 // and let u work on it, which to use should be chosen on case by case basis
96 'mysql://root@localhost/test', // the DSN
97 $options); // pass the options we had assigned up there
98
99 // add a new root element in the tree
100 $rootId = $tree->add(array( 'name'=>'myElement'));
101
102 // add an element under the new element we added
103 $id = $tree->add( array( 'name'=>'subElement') , $rootId );
104
105 // add another element under the parent element we added
106 $id = $tree->add( array( 'name'=>'anotherSubElement') , $rootId , $id );
107
108 // call 'setup', to build the inner array, so we can work on the structure using the
109 // given methods
110 $tree->setup();
111
112 dumpAllNicely( 'dump all after creation' );
113
114 // get the path of the last inserted element
115 dumpHelper( '$tree->getPath( '.$id.' )' , 'dump the path from "myElement/anotherSubElement"' );
116
117 $id = $tree->getIdByPath('myElement/subElement');
118 dumpHelper( '$tree->getParent('.$id.')' , 'dump the parent of "myElement/subElement"' , true );
119 // you can also use: $tree->data[$id]['parent']
120
121 $id = $tree->getIdByPath('myElement');
122 dumpHelper( '$tree->getChild('.$id.')' , 'dump the child of "myElement"' , true );
123 // you can also use: $tree->data[$id]['child']
124
125 $id = $tree->getIdByPath('myElement');
126 dumpHelper( '$tree->getChildren('.$id.')' , 'dump the children of "myElement"' );
127 // you can also use: $tree->data[$id]['children']
128
129 $id = $tree->getIdByPath('myElement/subElement');
130 dumpHelper( '$tree->getNext('.$id.')' , 'dump the "next" of "myElement/subElement"' , true );
131 // you can also use: $tree->data[$id]['next']
132
133 $id = $tree->getIdByPath('myElement/anotherSubElement');
134 dumpHelper( '$tree->getPrevious('.$id.')' , 'dump the "previous" of "myElement/anotherSubElement"' , true );
135 // you can also use: $tree->data[$id]['previous']
136
137 $id = $tree->getIdByPath('myElement');
138 $element = $tree->data[$id]['child']['next']['parent']; // refer to yourself again, in a very complicated way :-)
139 dumpHelper( '$element[\'id\']' , 'demo of using the internal array, for referencing tree-nodes, see the code' );
140
141 $id = $tree->getIdByPath('myElement');
142 $element = $tree->data[$id]['child']['next']; // refer to the second child of 'myElement'
143 dumpHelper( '$element[\'id\']' , 'demo2 of using the internal array, for referencing tree-nodes, see the code' );
144 /*
145 $id = $tree->getIdByPath('myElement/anotherSubElement');
146 $tree->move( $id , 0 );
147 $tree->setup(); // rebuild the structure again, since we had changed it
148 dumpAllNicely( 'dump all, after "myElement/anotherSubElement" was moved under the root' );
149
150 $moveId = $tree->getIdByPath('myElement');
151 $id = $tree->getIdByPath('anotherSubElement');
152 $tree->move( $moveId , $id );
153 $tree->setup(); // rebuild the structure again, since we had changed it
154 dumpAllNicely( 'dump all, after "myElement" was moved under the "anotherSubElement"' );
155 */
156 $tree->setRemoveRecursively(true);
157 $tree->remove( $rootId );
158 print '<font color="red">ALL ELEMENTS HAVE BEEN REMOVED (uncomment this part to keep them in the DB after running this test script)</font>';
159
160
161 print '<br><br>';print_r($query_history);
162
163 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed