So I took a few days to figure out how to create a parent child array
using left, right keys and parent child relationship. You can use this
code how ever you want.
You have to have a table that holds left, right and parent information.
<?php
class CBSTree {
public $tempArry;
public $parentString;
public $collectArry;
public $parentArry;
public function CBSTree()
{
$this->parentString;
$this->tempArry = array();
$this->collectArry = array();
$this->parentArry = array();
}
public function BuildTree($name)
{
/* setting up the database
*/
if(!$dbconnect = mysql_connect('localhost', 'root')) {
echo "Connection failed to the host 'localhost'.";
exit;
}
if (!mysql_select_db('UrDataBase')) {
echo "Cannot connect to database 'test'";
exit;
}
$table_id = 'dnt_cbs_table'; // Table name
$query = "SELECT node.name, node.CBSid, node.PARid
FROM $table_id AS node,
$table_id AS parent
WHERE node.L BETWEEN parent.L AND parent.R
AND parent.name = '$name'
ORDER BY node.L";
$result = mysql_query($query);
while($row=mysql_fetch_object($result)){
$this->parentArry[] = $row->PARid;
$this->collectArry[] = $row;
}
$this->CleanParentString();
return $this->tempArry[0][0];
}
/**
* CleanParentString()
* This will break the $this->parentString into an array, remove
duplicate numbers
* then reassemble the array into a string ($this->parentString)
ordered from lowest
* to highest
*/
public function CleanParentString()
{
$temp = array_unique($this->parentArry);
$this->parentArry = array_reverse($temp);
$this->GroupCommonChildObjects();
}
/**
* GroupCommonChildObjects()
* This will first get the last digit in the $this->parentString and
then loop through
* the array ($this->collectArry[]->parent) to make matches. If
there is a match then
* the object ($this->collectArray[]) is grouped in a temporary
array. This temporary
* array is then added to $this->tempArry;
*/
public function GroupCommonChildObjects()
{
for($e=0;$e<count($this->parentArry);$e++){
$arr = array();
for($i=0;$i<count($this->collectArry);$i++){
if($this->collectArry[$i]->PARid ==
$this->parentArry[$e]){
$arr[] = ($this->collectArry[$i]);
}
}
$this->tempArry[] = ($arr);
}
$this->DoTheWork();
}
public function FindParent($num,$childArry)
{
for($i=0;$i<count($this->tempArry);$i++){
for($x=0;$x<count($this->tempArry[$i]);$x++){
if($this->tempArry[$i][$x]->CBSid == $num){
$this->tempArry[$i][$x]->children = $childArry;
}
}
}
}
public function DoTheWork()
{
while(count($this->tempArry)>1){
$this->FindParent($this->tempArry[0][0]->PARid,
$this->tempArry[0]);
array_shift($this->tempArry);
}
}
}
--- In [email protected], "timgerr" <[EMAIL PROTECTED]> wrote:
>
> Hello all,
> I am working with nested arrayobjects and I cannot construct the
> needed data from my php/mysql backend.
>
> I am using nested sets
> (http://dev.mysql.com/tech-resources/articles/hierarchical-data.html),
> with left and right keys. I need to construct the data (in php) and
> pass it to flex (using a remote call). I need/want to get the data to
> flex and put it into an arraycollection. Has any one done this? If
> so, can you post some code? I have been banging my head on this for a
> long time and cannot get the data into the right format for an array
> collection.
>
> Thanks for the help,
> timgerr
>