1 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
include_once( 'DBQuery.class.php' );
|
|
|
4 |
|
|
|
5 |
/*
|
|
|
6 |
class reads a database table and converts it to an Hierarchical hash (aka, parent/child).
|
|
|
7 |
Table structure is assume to be:
|
|
|
8 |
create table menu (
|
|
|
9 |
id int unsigned not null,
|
|
|
10 |
parent_id int unsigned not null default 0,
|
|
|
11 |
other columns below
|
|
|
12 |
)
|
|
|
13 |
where id is a unique identifier and parent_id is points to id in another row (zero indicating it is a top level)
|
|
|
14 |
NOTE: column names may be changed. Also, while it is assumed id and parent_id are int's, it is quite likely it will
|
|
|
15 |
work with other column types, including varchar
|
|
|
16 |
Uses include any parent/child relationship, to an arbitrary depth. See class DBMenu for one example
|
|
|
17 |
|
|
|
18 |
This class was written to be generic. The algorithm used attempts to not take advantage of any special language
|
|
|
19 |
capabilities, and should be generic. However, various languages can optimize this capability by using capabilities unique
|
|
|
20 |
to the language in question. For an example, see David North's PERL implementation which utilizes references to allow for
|
|
|
21 |
a single pass to convert from an array to a Hierarchical structure. Taking advantage of this allows his script to run
|
|
|
22 |
use no additional memory (this algorithm will double the amount of memory used for the initial array) and does in a single
|
|
|
23 |
iterative pass what the recursive function copyChildren does.
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
class DBHierarchicalHash {
|
|
|
27 |
protected $tableName; // table name that data will be read from
|
|
|
28 |
protected $keyField; // name of the primary id field in table
|
|
|
29 |
protected $parentFieldName; // name of the field used to point to the parent of the current row
|
|
|
30 |
protected $rootNodevalue; // the value (default 0) of the indicator that a row has no parent. THIS MUST NOT EXIST in the id column of the table
|
|
|
31 |
protected $inputRecords; // storage for the records read in.
|
|
|
32 |
|
|
|
33 |
public function __construct( $tableName, $keyField = 'id', $parentFieldName = 'parent_id', $rootNodeValue = 0 ) {
|
|
|
34 |
// standard variable load
|
|
|
35 |
$this->tableName = $tableName;
|
|
|
36 |
$this->keyField = $keyField;
|
|
|
37 |
$this->parentFieldName = $parentFieldName;
|
|
|
38 |
$this->rootNodevalue = $rootNodeValue;
|
|
|
39 |
// load the data from the database into memory
|
|
|
40 |
// upon completion, will have data in a single hash of hashes (not hiearchy)
|
|
|
41 |
// will also create an emtpy entry for "element 0"
|
|
|
42 |
$this->loadData();
|
|
|
43 |
// find the children of each node
|
|
|
44 |
$this->findChildren();
|
|
|
45 |
// now that we know who all the children are, let's actually make them part of the structure
|
|
|
46 |
$this->inputRecords = $this->copyChildren($this->inputRecords[$this->rootNodevalue]);
|
|
|
47 |
// all we care about are the children of the root node, so simply assign that to inputRecords
|
|
|
48 |
$this->inputRecords = $this->inputRecords['children'];
|
|
|
49 |
} // function __construct
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
/*
|
|
|
53 |
Goes through each node and reverses the "who is my parent" to the parent's "who are my children"
|
|
|
54 |
It does not populate the children; it simply creates an empty array for each child to be processed
|
|
|
55 |
later.
|
|
|
56 |
*/
|
|
|
57 |
private function findChildren () {
|
|
|
58 |
// following loop takes inputRecords and creates (empty) elements for each child
|
|
|
59 |
foreach ($this->inputRecords as $key => $values) { // go through every node
|
|
|
60 |
if ( isset ($this->inputRecords[$key][$this->parentFieldName]) ) { // if it has a parent
|
|
|
61 |
// tell parent about itself
|
|
|
62 |
$this->inputRecords[$values[$this->parentFieldName]]['children'][$key] = array();
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
} // function findChildren
|
|
|
66 |
|
|
|
67 |
/*
|
|
|
68 |
simply returns inputRecords for processing by calling routine. This is the goal of this class, to create
|
|
|
69 |
this structure from the database structure defined above
|
|
|
70 |
*/
|
|
|
71 |
public function toHash () {
|
|
|
72 |
return $this->inputRecords;
|
|
|
73 |
} // function toHash
|
|
|
74 |
|
|
|
75 |
/*
|
|
|
76 |
loads data from database. The column defined in $this->keyField will become the index into the hash, and all
|
|
|
77 |
additional columns will simply be added to the array pointed by it.
|
|
|
78 |
An additional entry in the table will be created with $this->rootNodeValue as its index, to allow root level
|
|
|
79 |
items to have a parent.
|
|
|
80 |
*/
|
|
|
81 |
private function loadData ( ) {
|
|
|
82 |
$inputRows = new DBQuery( "select * from $this->tableName" );
|
|
|
83 |
$inputRows->run();
|
|
|
84 |
// $inputRows = $inputRows['data']; // we only care about the data
|
|
|
85 |
$this->inputRecords = array(); // initialize inputRecords to empty
|
|
|
86 |
// main loop, will read the query results in one row at a time
|
|
|
87 |
foreach ( $inputRows->returnData as $thisRow ) {
|
|
|
88 |
foreach ($thisRow as $key => $values) { // copy each value
|
|
|
89 |
if ($key != $this->keyField) { // unless it is the key field
|
|
|
90 |
// this is more complex as a parent of null might be used. Thus, we check for a null and, if
|
|
|
91 |
// it is used, we set it to the rootNodeValue
|
|
|
92 |
if ($key == $this->parentFieldName && ! $thisRow[$key] ) {
|
|
|
93 |
$this->inputRecords[$thisRow[$this->keyField]][$key] = $this->rootNodevalue;
|
|
|
94 |
} else {
|
|
|
95 |
$this->inputRecords[$thisRow[$this->keyField]][$key] = $thisRow[$key];
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
$this->inputRecords[$this->rootNodevalue] = array(); // create our empty root node
|
|
|
101 |
} // function loadData
|
|
|
102 |
|
|
|
103 |
# populates the hiearchy created in load
|
|
|
104 |
# NOTE: if the current node also has children, it will recursively do those
|
|
|
105 |
# main call is ©Children( rootNode )
|
|
|
106 |
private function copyChildren($currentNode) {
|
|
|
107 |
# for each child
|
|
|
108 |
//print "Looking at: "; print_r($currentNode);
|
|
|
109 |
if ( isset($currentNode['children']) ) {
|
|
|
110 |
foreach ($currentNode['children'] as $key => $values) {
|
|
|
111 |
# copy any children of this child table that may exist
|
|
|
112 |
$this->inputRecords[$key] = $this->copyChildren( $this->inputRecords[$key] );
|
|
|
113 |
# copy each value from the node to this sub-node
|
|
|
114 |
foreach ($this->inputRecords[$key] as $child => $childValue) {
|
|
|
115 |
if ($child != $this->parentFieldName ) {
|
|
|
116 |
$currentNode['children'][$key][$child] = $childValue;
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
return $currentNode;
|
|
|
122 |
} // function copyChildren
|
|
|
123 |
} // class DBHierarchicalHash
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
?>
|