I'm using the JsTree plugin to make a pull-down menu by levels, I found a reference code and I managed to add child nodes, but now I want to add grandchild nodes.
I have 4 tables, but I want the following in the menu: "carretas" HAS plan_estudios > plan_estudios HAS "carga" > ...and carga HAS "...other table"
These are my Outputs (what I have and what I want to obtain)
AND This is my menu, for now "carrera" only gets "plan_estudios"
my code is as follows.
$node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
//A las cols "nombre" asignar Alias "text", ...lo mismo con parent_id, etc para que jsTree lo acepte
/* Statement 1: Show "Carreras" */
$sql = "SELECT idcarrera, nombre AS text FROM Escolares.carrera ";
$res = $pdo->prepare($sql);
$res->execute();
#$res = $res->fetch(PDO::FETCH_ASSOC);
/* End Statement 1 */
# if($res->num_rows <=0){
//add condition when result is zero
# } else {
//iterate on results row and create new index array of data
while( $row = $res->fetch(PDO::FETCH_ASSOC) ) {
$data[] = $row;
}
$itemsByReference = array();
// Build array of item references:
foreach($data as $key => &$item) {
$itemsByReference[$item['idcarrera']] = &$item;
// Children array:
$itemsByReference[$item['idcarrera']]['children'] = array();
// Empty data class (so that json_encode adds "data: {}" )
$itemsByReference[$item['idcarrera']]['data'] = new StdClass();
// Set items as children of the relevant parent item.
foreach($data as $key => &$item){
/*****/ #Aqui tenia un SubQuery que mostraba los planes de cada carrera
} //end foreach
}
/* Statement 2: Show children nodes: Planes de estudios */
$sqlChild = "SELECT idcarrera AS parent_id, clave AS text FROM Escolares.plan_estudios"; /* WHERE idcarrera='".$item['idcarrera']."' */
$planes = $pdo->prepare($sqlChild);
$planes->execute();
while($p = $planes->fetch()){
//Almacenar Nodos Hijos
while( $row2 = $planes->fetch(PDO::FETCH_ASSOC) ) {
$data2[] = $row2;
}
// Build array of item references:
foreach($data2 as $key => &$p) {
if($p['parent_id'] && isset($itemsByReference[$p['parent_id']])){
$itemsByReference [$p['parent_id']]['children'][] = &$p; //cambie $item por $p
/*
* DOES FUNCTION
*/
}
}
}
/* End Statement 2 */
/*
* HERE DOWN I WANT TO ADD GRANDCHILDREN.. THE CODE HERE DOWN IS THE ONE THAT DOES NOT WORKS
*/
$sqlCargas = "SELECT idcarga as id, clave as text, idplan_estudios as parent_id FROM Escolares.carga "; //WHERE idplan_estudios='1'"; /* WHERE idcarrera='".$item['idcarrera']."' */
$cargas = $pdo->prepare($sqlCargas);
$cargas->execute();
$cargas -> fetchAll();
while($c = $cargas->fetch()){
//Almacenar Nodos Hijos
while( $row3 = $cargas->fetch(PDO::FETCH_ASSOC) ) {
$data3[] = $row3;
}
// Build array of item references:
foreach($data3 as $key => &$c) {
$itemsByReference[$c['idcarga']] = &$c;
// Children array:
$itemsByReference[$c['idcarga']]['grandchildren'] = array();
}
// Build array of item references:
foreach($data3 as $key => &$c) {
if($c['parent_id'] && isset($itemsByReference[$c['parent_id']])){
$itemsByReference [$c['parent_id']]['grandchildren'][] = &$c;
}
}
}