如何将孙子节点添加到JsTree PHP SQL?

时间:2018-12-14 19:46:38

标签: php mysql json pdo jstree

我正在使用JsTree插件按级别创建下拉菜单,我找到了一个引用code,并且设法添加了子节点,但是现在我想添加孙子节点。

我有4张桌子,但我想在菜单中输入以下内容:  “ carretas ”具有 plan_estudios > plan_estudios 具有“ carga ”> ...和 carga 具有HAS“ ... other table

these are my tables

这些是我的输出 (what I have and what I want to obtain)

AND This is my menu, for now "carrera" only gets "plan_estudios"

我的代码如下。

            $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;
                    }
                }

            }

0 个答案:

没有答案