1 |
joko |
1.1 |
<?php |
2 |
|
|
// |
3 |
|
|
// Id: Memory_DBsimple.php,v 1.2 2003/01/30 17:43:41 cain Exp |
4 |
|
|
// $Id: Memory_DBsimple.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 ' '; |
47 |
|
|
print $aElement['name'].' ===> '; |
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' => 'test_tree', |
83 |
|
|
'order' => 'id' // when reading the data from the db sort them by id, this is only for ensuring |
84 |
|
|
// for 'getNext' of "myElement/subElement" in this example to find "myElement/anotherSubElement" |
85 |
|
|
// you can simply sort it by "name" and it would be in alphabetical order |
86 |
|
|
); |
87 |
|
|
|
88 |
|
|
// calling 'setupMemory' means to retreive a class, which works on trees, |
89 |
|
|
// that are temporarily stored in the memory, in an array |
90 |
|
|
// this means the entire tree is available at all time |
91 |
|
|
// consider the resource usage and it's not to suggested to work |
92 |
|
|
// on huge trees (upto 1000 elements it should be ok, depending on your environment and requirements) |
93 |
|
|
// using 'setupMemory' |
94 |
|
|
$tree = Tree::setupMemory( 'DBsimple', // use the simple DB schema |
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 |
|
|
$parentId = $tree->add(array( 'name'=>'myElement')); |
100 |
|
|
|
101 |
|
|
// add an element under the new element we added |
102 |
|
|
$id = $tree->add(array( 'name'=>'subElement') , $parentId ); |
103 |
|
|
|
104 |
|
|
// add another element under the parent element we added |
105 |
|
|
$id = $tree->add(array( 'name'=>'anotherSubElement') , $parentId ); |
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 |
|
|
print "tree->getIdByPath('myElement/subElement')=".$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(0); |
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 |
|
|
?> |