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 ( ) {
|
6 |
rodolico |
82 |
try {
|
|
|
83 |
$inputRows = new DBQuery( "select * from $this->tableName" );
|
|
|
84 |
$inputRows->run();
|
|
|
85 |
} catch ( Exception $e ) {
|
|
|
86 |
print '<pre>' . $e->getMessage() . "\n" . print_r( $inputRows ) . '</pre>';
|
|
|
87 |
die;
|
|
|
88 |
}
|
1 |
rodolico |
89 |
// $inputRows = $inputRows['data']; // we only care about the data
|
|
|
90 |
$this->inputRecords = array(); // initialize inputRecords to empty
|
|
|
91 |
// main loop, will read the query results in one row at a time
|
|
|
92 |
foreach ( $inputRows->returnData as $thisRow ) {
|
|
|
93 |
foreach ($thisRow as $key => $values) { // copy each value
|
|
|
94 |
if ($key != $this->keyField) { // unless it is the key field
|
|
|
95 |
// this is more complex as a parent of null might be used. Thus, we check for a null and, if
|
|
|
96 |
// it is used, we set it to the rootNodeValue
|
|
|
97 |
if ($key == $this->parentFieldName && ! $thisRow[$key] ) {
|
|
|
98 |
$this->inputRecords[$thisRow[$this->keyField]][$key] = $this->rootNodevalue;
|
|
|
99 |
} else {
|
|
|
100 |
$this->inputRecords[$thisRow[$this->keyField]][$key] = $thisRow[$key];
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
$this->inputRecords[$this->rootNodevalue] = array(); // create our empty root node
|
|
|
106 |
} // function loadData
|
|
|
107 |
|
|
|
108 |
# populates the hiearchy created in load
|
|
|
109 |
# NOTE: if the current node also has children, it will recursively do those
|
|
|
110 |
# main call is ©Children( rootNode )
|
|
|
111 |
private function copyChildren($currentNode) {
|
|
|
112 |
# for each child
|
|
|
113 |
//print "Looking at: "; print_r($currentNode);
|
|
|
114 |
if ( isset($currentNode['children']) ) {
|
|
|
115 |
foreach ($currentNode['children'] as $key => $values) {
|
|
|
116 |
# copy any children of this child table that may exist
|
|
|
117 |
$this->inputRecords[$key] = $this->copyChildren( $this->inputRecords[$key] );
|
|
|
118 |
# copy each value from the node to this sub-node
|
|
|
119 |
foreach ($this->inputRecords[$key] as $child => $childValue) {
|
|
|
120 |
if ($child != $this->parentFieldName ) {
|
|
|
121 |
$currentNode['children'][$key][$child] = $childValue;
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
return $currentNode;
|
|
|
127 |
} // function copyChildren
|
|
|
128 |
} // class DBHierarchicalHash
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
?>
|