dongzouche9108 2018-12-14 19:46
浏览 67

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

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 tables

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

            }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab中使用gurobi时报错
    • ¥15 WPF 大屏看板表格背景图片设置
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂