Recursion through relevant objects

I have a set of organizations and their members. All organizations have board members, and many board members are on the board of more than one organization.

I use JIT Hypertree to illustrate their relationship. The Hypertree JIT scheme requires that one element be the parent of all and drawn based on a single JSON array.

I would like the request for re-centering and refilling the chart to be based on the change. Then 2 levels would be good, but I could not decide how to do it.

The code that I am now manually typing for the three levels from the initial organization, but what I want is to re-curse through all the related entries.

So, it will start with Org and add an Org array for children (board members). Then take all the boards (except the current Org) for each board member and add them as children of the board member.

This will continue until each track ends — presumably with a member of the board who owns only one board.

Anyone have any tips on how to create this array and avoid duplicates?

$board = $center->board(); $top['id'] = $center->ID; $top['name'] = $center->Org; $top['children'] = array(); if ($board) { foreach ($board as $b) { $child['id'] = $b->ID; $child['name'] = (strlen(trim($b->Last)) > 0) ? $b->First . ' ' . $b->Last : 'Unknown'; $child['data']['orgname'] = $center->Org; $child['data']['relation'] = $b->Role; $child['data']['occupation'] = $b->Occupation; $child['children'] = array(); $childboards = $b->boards(); if ($childboards) { foreach ($childboards as $cb) { $gchild['id'] = $cb->ID; $gchild['name'] = $cb->Org; $gchild['data']['orgname'] = (strlen(trim($b->Last)) > 0) ? $b->First . ' ' . $b->Last : 'Unknown'; $gchild['children'] = array(); $childboardmembers = $cb->board(); if ($childboardmembers) { foreach ($childboardmembers as $cbm) { $ggchild['id'] = $cbm->ID; $ggchild['name'] = (strlen(trim($cbm->Last)) > 0) ? $cbm->First . ' ' . $cbm->Last : 'Unknown'; $ggchild['data']['orgname'] = $cb->Org; $ggchild['data']['relation'] = $cbm->Role; $ggchild['data']['occupation'] = $cbm->Occupation; $ggchild['children'] = array(); $gchild['children'][]= $ggchild; }} $child['children'][]= $gchild; }} $top['children'][] = $child; } } $top['data'] = array(); $top['data']['description'] = $center->Desc; echo json_encode($top); 

// Edit 2011.10.24 In response to Re hakre, My data structure is a table of organizations with unique identifiers, a People table with unique identifiers, and then a bridge table for the two defining Organization (Entity) and Person and the role that Person plays in Entity. Typical many-to-many. No sub-boards at all. I made an image that now seems pointless, but I will add it below.

The data structure of the JIT library is a bit of a nut (for me) in that it looks like this in the example of its group:

 Top: Nine Inch Nails Child: Jerome Dillon Child: Howlin Maggie (another band) {all the bands' members and then all of their bands...} 

Thus, an organization (group) is treated as if it is a Person, although it consists of several Persons. And when I recursively use the code above, I get (I think) a scary bloat, but JSON is working correctly despite the bloat.

JSON Example and Visualization Example // End Edit

schema

+4
source share
3 answers

It is difficult to answer your question in the sense that your data structure is largely unknown.

For a graphical representation, you only need to provide a simple relationship, if I understand correctly:

 *- Parent +- Child +- Child ... `- Child 

Your data structure has a different format, I do not know specifically, but it is something like:

 Org <-- 1:n --> Board Board <-- n:n --> Board # If you have sub-boards Board <-- n:n --> Member 

No matter what your data is presented, to map or transfer data to the required structure for graphical presentation, you will need some functions that take care of this.

To do this, you need to divide the classification / type between both and specific keys so that you can search for the necessary data from the event to return data. For instance:

 if (request_parent_is_org()) { $id = request_parent_id(); $parent = data_get_board($id); $parent->addChildrent(data_get_board_children($id)); } else { ... # All the other decisions you need to take based on request type } view_response_to_json($parent); 
+2
source

What you have with your many-to-many data model is a graph . JIT is for trees .

In other words, the JIT will not correctly show the intersecting lines that are presented in the data when one person is connected to several organizations.

I would recommend the correct visualization of the network diagram - D3.js has an excellent implementation for modern browsers.

The JSON data format that it uses is actually easier to implement taking into account your table structure - for all organizations and people you define objects:

 { "name": "Mme.Hucheloup", "group": 1 }, { "name": "Afton School Board", "group": 2 } 

And for each association in your association table, you define join objects that join them together:

 { "source": 1, "target": 2 }, 

Bizarre coding in D3 takes care of the rest. Good luck

+2
source

You can use the function below:

 function get_orgs_and_childs ($child_id, $found = array()) { array_push ($found, $child['name']); if($child['children'])){ $found[] = get_parents($child['id'], $found); } return $found; } 

Name it using:

 $orgs = get_orgs_and_childs($child['id']); 
+2
source

All Articles