Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
//  $Id: Memory_DBnested.php,v 1.3 2004/12/21 19:59:20 dufuz Exp $
3
 
4
ini_set('error_reporting',E_ALL);
5
 
6
    /**
7
    *   this is a helper function, so i dont have to write so many prints :-)
8
    *   @param  array   $para   the result returned by some method, that will be dumped
9
    *   @param  string  $string the explaining string
10
    */
11
    function dumpHelper($para, $string = '', $addArray = false)
12
    {
13
        global $tree, $element;
14
 
15
        if ($addArray) {
16
            eval( "\$res=array(".$para.');' );
17
        } else {
18
            eval( "\$res=".$para.';' );
19
        }
20
        echo  '<b>'.$para.' </b><i><u><font color="#008000">'.$string.'</font></u></i><br />';
21
        // this method dumps to the screen, since print_r or var_dump dont
22
        // work too good here, because the inner array is recursive
23
        // well, it looks ugly but one can see what is meant :-)
24
        $tree->varDump($res);
25
        echo '<br />';
26
 
27
    }
28
 
29
    /**
30
    *   dumps the entire structure nicely
31
    *   @param  string  $string the explaining string
32
    */
33
    function dumpAllNicely($string = '')
34
    {
35
        global $tree;
36
 
37
        echo '<i><u><font color="#008000">'.$string.'</font></u></i><br />';
38
        $all = $tree->getNode();   // get the entire structure sorted as the tree is, so we can simply foreach through it and show it
39
        foreach($all as $aElement) {
40
            for ($i = 0; $i < $aElement['level']; $i++) {
41
                echo '&nbsp; &nbsp; ';
42
            }
43
            echo '<font color="red">'.$aElement['name'].'</font> ===&gt; ';
44
            $tree->varDump(array($aElement));
45
        }
46
        echo '<br />';
47
 
48
    }
49
 
50
 
51
    /*
52
 
53
        use this to build the db table
54
 
55
        CREATE TABLE Memory_Nested_DB (
56
            id int(11) NOT NULL default '0',
57
            name varchar(255) NOT NULL default '',
58
            l int(11) NOT NULL default '0',
59
            r int(11) NOT NULL default '0',
60
            parent int(11) NOT NULL default '0',
61
            comment 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' => 'Memory_Nested_DB'
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
    // use the nested DB schema, which is actually implemented in Dynamic/DBnested
91
    // the class Memory/DBnested is only kind of a wrapper to read the entire tree
92
    // and let u work on it, which to use should be chosen on case by case basis
93
 
94
    // using 'setupMemory'
95
    $tree = Tree::setupMemory('DBnested',
96
                                'mysql://root@localhost/tree_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
    echo '<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
echo '<br /><br />';
162
 
163
?>