Line 27... |
Line 27... |
27 |
protected $tableName; // table name that data will be read from
|
27 |
protected $tableName; // table name that data will be read from
|
28 |
protected $keyField; // name of the primary id field in table
|
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
|
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
|
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.
|
31 |
protected $inputRecords; // storage for the records read in.
|
- |
|
32 |
protected $dbConnection; // database connector, Class DBQuery
|
32 |
|
33 |
|
33 |
public function __construct( $tableName, $keyField = 'id', $parentFieldName = 'parent_id', $rootNodeValue = 0 ) {
|
34 |
public function __construct( $dbConnect, $tableName, $keyField = 'id', $parentFieldName = 'parent_id', $rootNodeValue = 0 ) {
|
34 |
// standard variable load
|
35 |
// standard variable load
|
35 |
$this->tableName = $tableName;
|
36 |
$this->tableName = $tableName;
|
36 |
$this->keyField = $keyField;
|
37 |
$this->keyField = $keyField;
|
37 |
$this->parentFieldName = $parentFieldName;
|
38 |
$this->parentFieldName = $parentFieldName;
|
38 |
$this->rootNodevalue = $rootNodeValue;
|
39 |
$this->rootNodevalue = $rootNodeValue;
|
- |
|
40 |
$this->dbConnection = $dbConnect;
|
39 |
// load the data from the database into memory
|
41 |
// load the data from the database into memory
|
40 |
// upon completion, will have data in a single hash of hashes (not hiearchy)
|
42 |
// upon completion, will have data in a single hash of hashes (not hiearchy)
|
41 |
// will also create an emtpy entry for "element 0"
|
43 |
// will also create an emtpy entry for "element 0"
|
42 |
$this->loadData();
|
44 |
$this->loadData();
|
43 |
// find the children of each node
|
45 |
// find the children of each node
|
Line 78... |
Line 80... |
78 |
An additional entry in the table will be created with $this->rootNodeValue as its index, to allow root level
|
80 |
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.
|
81 |
items to have a parent.
|
80 |
*/
|
82 |
*/
|
81 |
private function loadData ( ) {
|
83 |
private function loadData ( ) {
|
82 |
try {
|
84 |
try {
|
83 |
$inputRows = new DBQuery( "select * from $this->tableName" );
|
85 |
$inputRows = $this->dbConnection->doSQL( "select * from $this->tableName" );
|
84 |
$inputRows->run();
|
- |
|
85 |
} catch ( Exception $e ) {
|
86 |
} catch ( Exception $e ) {
|
86 |
print '<pre>' . $e->getMessage() . "\n" . print_r( $inputRows ) . '</pre>';
|
87 |
print '<pre>' . $e->getMessage() . "\n$this->tableName\n" . print_r( $inputRows, true ) . '</pre>';
|
87 |
die;
|
88 |
die;
|
88 |
}
|
89 |
}
|
89 |
// $inputRows = $inputRows['data']; // we only care about the data
|
90 |
// print "<pre>InputRows" . print_r( $inputRows, true ) . '</pre>';
|
90 |
$this->inputRecords = array(); // initialize inputRecords to empty
|
91 |
$this->inputRecords = array(); // initialize inputRecords to empty
|
91 |
// main loop, will read the query results in one row at a time
|
92 |
// main loop, will read the query results in one row at a time
|
92 |
foreach ( $inputRows->returnData as $thisRow ) {
|
93 |
foreach ( $inputRows['returnData'] as $thisRow ) {
|
93 |
foreach ($thisRow as $key => $values) { // copy each value
|
94 |
foreach ($thisRow as $key => $values) { // copy each value
|
94 |
if ($key != $this->keyField) { // unless it is the key field
|
95 |
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 |
// 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 |
// it is used, we set it to the rootNodeValue
|
97 |
if ($key == $this->parentFieldName && ! $thisRow[$key] ) {
|
98 |
if ($key == $this->parentFieldName && ! $thisRow[$key] ) {
|