dongyao4003 2015-05-21 14:28
浏览 126
已采纳

将数据从jstree插入数据库

I'm having certain trouble to figure how I can receive a set of ids or the complete node info and use that data to insert the correspondent row into the db.

Why is this happening? Well, I have the following hierarchy projeto>uc>ambiente>secao>med. In my JS tree I'm using lazy loading so let's say the user select a 'projeto' and them submits all I got is the 'projeto' id, so that's easy and I know that I have to insert on the db all it's childs and their childs. But let's say the user select a particular 'ambiente' or a specific 'secao' all I get is a single id or a single node data, but to insert that info I need to insert all it's parents data before I can insert it on the db.

example 1 single 'projeto' selected data.

[{"id":"projeto_1","text":"Pr\u00e9dios P\u00fablicos","icon":"fa fa-folder icon-lg icon-state-info","parent":"#","parents":["#"],"data":{"id_mobile":"1"},"state":{"loaded":"false","opened":"false","selected":"true","disabled":"false"},"li_attr":{"id":"projeto_1"},"a_attr":{"href":"#"},"original":{"id":"projeto_1","text":"Pr\u00e9dios P\u00fablicos","icon":"fa fa-folder icon-lg icon-state-info"}}]

example 2 single 'ambiente' selected, may have 'secao' childs or not.

[{"id":"ambiente_4","text":"protocolo","icon":"fa fa-folder icon-lg icon-state-info","parent":"uc_1","parents":["uc_1","projeto_1","#"],"data":{"id_ambiente_mobile":"4"},"state":{"loaded":"false","opened":"false","selected":"true","disabled":"false"},"li_attr":{"id":"ambiente_4"},"a_attr":{"href":"#"},"original":{"id":"ambiente_4","text":"protocolo","icon":"fa fa-folder icon-lg icon-state-info","type":"ambiente"}}]

example 3 single 'secao' selected data.

[{"id":"secao_5","text":"1 Lumin\u00e1ria(s) LFT 1X40W","icon":"fa fa-folder icon-lg icon-state-info","parent":"ambiente_5","parents":["ambiente_5","uc_1","projeto_1","#"],"data":{"id_secao_mobile":"5"},"state":{"loaded":"false","opened":"false","selected":"true","disabled":"false"},"li_attr":{"id":"secao_5"},"a_attr":{"href":"#"},"original":{"id":"secao_5","text":"1 Lumin\u00e1ria(s) LFT 1X40W","icon":"fa fa-folder icon-lg icon-state-info","type":"secao"}},{"id":"ambiente_5","text":"Recep\u00e7\u00e3o","icon":"fa fa-folder icon-lg icon-state-info","parent":"uc_1","parents":["uc_1","projeto_1","#"],"children":["secao_5"],"children_d":["secao_5"],"data":{"id_ambiente_mobile":"5"},"state":{"loaded":"true","opened":"true","selected":"true","disabled":"false","loading":"false"},"li_attr":{"id":"ambiente_5"},"a_attr":{"href":"#"},"original":{"id":"ambiente_5","text":"Recep\u00e7\u00e3o","icon":"fa fa-folder icon-lg icon-state-info","type":"ambiente"}}]

all the data above is the data passed to the php file. so I just jso_encoded and posted it here.

So what I need is to insert the selected nodes in a db, but considering that if the parent node is not loaded on the tree it may have childs. And off course a solution for the case when I select a child and need to iterate all backup them insert it parent dependents before insert the child (last two examples).

Hope you guys can help me. If any clarification is needed just ask for.

Thank you.

  • 写回答

1条回答 默认 最新

  • dqed19166 2015-05-22 13:02
    关注

    Ok guys half the way done. created the following code:

    //take the actual node.
    for ($i = 0; $i < count($ids); $i++) {
    
        //if the actual node is loaded and opened.
        if (($ids[$i]['state']['loaded'] == true) && ($ids[$i]['state']['opened'] == true)) {
            //then the node is inserted in the db.
            checaNo ($ids[$i]);
            //and the iteration jump all it's selected children. This is because checaNo already insert all actual node children.
            $i = count($ids[$i]['children'])+1;
    
        }
        //the actual node has not any children or it's children is not loaded.
        else
        {
            //insert the node and it's children if they exists. Then go to the next node.
            checaNo($ids[$i]);
        }
    
    }
    

    now the problem is, let's say I have selected a "ambiente" in as a new "projeto" so before I even can insert the "ambiente" data I need to create it's parents. which in this example is "projeto" and "uc", "projeto" needs to be inserted, then "uc" needs to be inserted, only then I can insert "ambiente" on the db.

    EDIT: Ok here is what I did to solve this problem. creted the following function

    function checaPai ($no) {
    
        global $data;
    
        $nivel = count($no['parents'])-1;
    
        switch ($nivel) {
            case 0;
                break;
            case 1;
                $args_projeto = new stdClass();
                $id_projeto = explode("_", $no['parent']);
                $args_projeto->where = "data_hora_importacao = '$data' AND id_mobile = '" . $id_projeto[1]."'";
    
                $projeto = getMobileProjeto($args_projeto);
    
                $args_projeto_online = new stdClass();
                $args_projeto_online->where = "id = '" . $projeto[0]->id_online."'";
                $projeto_online = getOnlineProjeto($args_projeto_online);
    
                if (count($projeto_online) == 0) {
    
                    $id_projeto = insereProjeto($args_projeto, false);
    
                    return $id_projeto;
                }
                else {
    
                    return $projeto_online[0]->id;
    
                }
    
                break;
            case 2;
                $args_uc = new stdClass();
                $id_uc = explode("_", $no['parent']);
                $args_uc->where = "data_hora_importacao = '$data' AND id_uc_mobile = '" . $id_uc[1]."'";
                $uc = getMobileUC($args_uc);
    
                $args_uc_online = new stdClass();
                $args_uc_online->where = "contrato = '" . $uc[0]->contrato."'";
                $uc_online = getOnlineUC($args_uc_online);
    
                if (count($uc_online) == 0) {
    
    
                    $no_uc = array();
                    $no_uc['parent'] = $uc->projeto;
                    $id_uc = checaPai($no_uc);
                    return $id_uc;
                }
                else {
    
                    return $uc_online[0]->id;
                }
    
                break;
            case 3;
                break;
            case 4;
                break;
        }
    }
    

    The function above check for it's parent existence and then is inserted or created and all it's childs. When the child has for example 2 or 3 levels then the function call itself and return the parent id. That's it.

    It's incomplete and terrible ugly but for the sake of clarification and maybe if someone has the same problem they can see how I figured it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算