1 |
<?php |
2 |
// |
3 |
// $Id: Memory_DBsimple.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 $aElement['name'].' ===> '; |
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' => 'test_tree', |
82 |
'order' => 'id' // when reading the data from the db sort them by id, this is only for ensuring |
83 |
// for 'getNext' of "myElement/subElement" in this example to find "myElement/anotherSubElement" |
84 |
// you can simply sort it by "name" and it would be in alphabetical order |
85 |
); |
86 |
|
87 |
// calling 'setupMemory' means to retreive a class, which works on trees, |
88 |
// that are temporarily stored in the memory, in an array |
89 |
// this means the entire tree is available at all time |
90 |
// consider the resource usage and it's not to suggested to work |
91 |
// on huge trees (upto 1000 elements it should be ok, depending on your environment and requirements) |
92 |
// using 'setupMemory' |
93 |
$tree = Tree::setupMemory( 'DBsimple', // use the simple DB schema |
94 |
'mysql://root@localhost/test', // the DSN |
95 |
$options); // pass the options we had assigned up there |
96 |
|
97 |
// add a new root element in the tree |
98 |
$parentId = $tree->add(array( 'name'=>'myElement')); |
99 |
|
100 |
// add an element under the new element we added |
101 |
$id = $tree->add(array( 'name'=>'subElement') , $parentId ); |
102 |
|
103 |
// add another element under the parent element we added |
104 |
$id = $tree->add(array( 'name'=>'anotherSubElement') , $parentId ); |
105 |
|
106 |
// call 'setup', to build the inner array, so we can work on the structure using the |
107 |
// given methods |
108 |
$tree->setup(); |
109 |
|
110 |
dumpAllNicely( 'dump all after creation' ); |
111 |
|
112 |
// get the path of the last inserted element |
113 |
dumpHelper( '$tree->getPath( '.$id.' )' , 'dump the path from "myElement/anotherSubElement"' ); |
114 |
|
115 |
print "tree->getIdByPath('myElement/subElement')=".$id = $tree->getIdByPath('myElement/subElement'); |
116 |
dumpHelper( '$tree->getParent('.$id.')' , 'dump the parent of "myElement/subElement"' , true ); |
117 |
// you can also use: $tree->data[$id]['parent'] |
118 |
|
119 |
$id = $tree->getIdByPath('myElement'); |
120 |
dumpHelper( '$tree->getChild('.$id.')' , 'dump the child of "myElement"' , true ); |
121 |
// you can also use: $tree->data[$id]['child'] |
122 |
|
123 |
$id = $tree->getIdByPath('myElement'); |
124 |
dumpHelper( '$tree->getChildren('.$id.')' , 'dump the children of "myElement"' ); |
125 |
// you can also use: $tree->data[$id]['children'] |
126 |
|
127 |
$id = $tree->getIdByPath('myElement/subElement'); |
128 |
dumpHelper( '$tree->getNext('.$id.')' , 'dump the "next" of "myElement/subElement"' , true ); |
129 |
// you can also use: $tree->data[$id]['next'] |
130 |
|
131 |
$id = $tree->getIdByPath('myElement/anotherSubElement'); |
132 |
dumpHelper( '$tree->getPrevious('.$id.')' , 'dump the "previous" of "myElement/anotherSubElement"' , true ); |
133 |
// you can also use: $tree->data[$id]['previous'] |
134 |
|
135 |
$id = $tree->getIdByPath('myElement'); |
136 |
$element = $tree->data[$id]['child']['next']['parent']; // refer to yourself again, in a very complicated way :-) |
137 |
dumpHelper( '$element[\'id\']' , 'demo of using the internal array, for referencing tree-nodes, see the code' ); |
138 |
|
139 |
$id = $tree->getIdByPath('myElement'); |
140 |
$element = $tree->data[$id]['child']['next']; // refer to the second child of 'myElement' |
141 |
dumpHelper( '$element[\'id\']' , 'demo2 of using the internal array, for referencing tree-nodes, see the code' ); |
142 |
|
143 |
$id = $tree->getIdByPath('myElement/anotherSubElement'); |
144 |
$tree->move( $id , 0 ); |
145 |
$tree->setup(); // rebuild the structure again, since we had changed it |
146 |
dumpAllNicely( 'dump all, after "myElement/anotherSubElement" was moved under the root' ); |
147 |
|
148 |
$moveId = $tree->getIdByPath('myElement'); |
149 |
$id = $tree->getIdByPath('anotherSubElement'); |
150 |
$tree->move( $moveId , $id ); |
151 |
$tree->setup(); // rebuild the structure again, since we had changed it |
152 |
dumpAllNicely( 'dump all, after "myElement" was moved under the "anotherSubElement"' ); |
153 |
|
154 |
$tree->setRemoveRecursively(true); |
155 |
$tree->remove(0); |
156 |
print '<font color="red">ALL ELEMENTS HAVE BEEN REMOVED (uncomment this part to keep them in the DB after running this test script)</font>'; |
157 |
|
158 |
?> |