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