将jstree中的数据插入数据库
inserting data from jstree into database
我在弄清楚如何接收一组 ID 或完整的节点信息并使用该数据将对应行插入数据库时遇到了一些麻烦。
为什么会这样?好吧,我有以下层次结构 projeto>uc>ambiente>secao>med。在我的 JS 树中,我使用延迟加载,所以假设用户 select a 'projeto' 并且他们提交我得到的只是 'projeto' id,所以这很容易,我知道我有在数据库中插入所有 childs 和它们的 childs。但是假设用户 select 一个特定的 'ambiente' 或一个特定的 'secao' 我得到的只是一个 id 或一个节点数据,但是要插入该信息我需要插入所有的 parents 数据,然后我才能将其插入数据库。
示例 1 单个 'projeto' selected 数据。
[{"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"}}]
示例 2 单曲 'ambiente' selected,可能有 'secao' child 或没有。
[{"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"}}]
示例 3 单个 'secao' selected 数据。
[{"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"}}]
以上所有数据都是传给php文件的数据。所以我只是 jso_encoded 并张贴在这里。
所以我需要的是在数据库中插入 selected 节点,但是考虑到如果 parent 节点没有加载到树上它可能有 childs .当然,当我 select a child 并且需要迭代所有备份时,在插入 child 之前将其插入 parent 依赖项(最后两个示例)。
希望大家能帮帮我。如果需要任何说明,请询问。
谢谢。
好的,伙计们,完成了一半。创建了以下代码:
//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]);
}
}
现在的问题是,假设我选择了一个 "ambiente" 作为新的 "projeto",所以在我插入 "ambiente" 数据之前,我需要创建它 parent秒。在这个例子中是"projeto"和"uc",需要插入"projeto",然后需要插入"uc",然后我才能插入"ambiente"数据库
编辑:好的,这就是我为解决这个问题所做的工作。创建了以下函数
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;
}
}
上面的函数检查它是否 parent 存在,然后被插入或创建,所有它都是 child。例如,当 child 有 2 或 3 个级别时,函数会调用自身,return 会调用 parent id。就是这样。
它不完整且非常丑陋,但为了澄清起见,也许如果有人遇到同样的问题,他们可以看看我是如何解决的。
我在弄清楚如何接收一组 ID 或完整的节点信息并使用该数据将对应行插入数据库时遇到了一些麻烦。
为什么会这样?好吧,我有以下层次结构 projeto>uc>ambiente>secao>med。在我的 JS 树中,我使用延迟加载,所以假设用户 select a 'projeto' 并且他们提交我得到的只是 'projeto' id,所以这很容易,我知道我有在数据库中插入所有 childs 和它们的 childs。但是假设用户 select 一个特定的 'ambiente' 或一个特定的 'secao' 我得到的只是一个 id 或一个节点数据,但是要插入该信息我需要插入所有的 parents 数据,然后我才能将其插入数据库。
示例 1 单个 'projeto' selected 数据。
[{"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"}}]
示例 2 单曲 'ambiente' selected,可能有 'secao' child 或没有。
[{"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"}}]
示例 3 单个 'secao' selected 数据。
[{"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"}}]
以上所有数据都是传给php文件的数据。所以我只是 jso_encoded 并张贴在这里。
所以我需要的是在数据库中插入 selected 节点,但是考虑到如果 parent 节点没有加载到树上它可能有 childs .当然,当我 select a child 并且需要迭代所有备份时,在插入 child 之前将其插入 parent 依赖项(最后两个示例)。
希望大家能帮帮我。如果需要任何说明,请询问。
谢谢。
好的,伙计们,完成了一半。创建了以下代码:
//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]);
}
}
现在的问题是,假设我选择了一个 "ambiente" 作为新的 "projeto",所以在我插入 "ambiente" 数据之前,我需要创建它 parent秒。在这个例子中是"projeto"和"uc",需要插入"projeto",然后需要插入"uc",然后我才能插入"ambiente"数据库
编辑:好的,这就是我为解决这个问题所做的工作。创建了以下函数
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;
}
}
上面的函数检查它是否 parent 存在,然后被插入或创建,所有它都是 child。例如,当 child 有 2 或 3 个级别时,函数会调用自身,return 会调用 parent id。就是这样。
它不完整且非常丑陋,但为了澄清起见,也许如果有人遇到同样的问题,他们可以看看我是如何解决的。