2015-05-21 33 views
1

我很難確定如何接收一組id或完整的節點信息並使用該數據將對應行插入到數據庫中。將數據從jstree插入到數據庫中

這是怎麼發生的?那麼,我有以下層次projeto> uc> ambiente> secao> med。在我的JS樹中我使用延遲加載,所以讓我們說用戶選擇'projeto',他們提交我得到的所有'projeto'id,所以這很容易,我知道我必須在數據庫上插入所有它的孩子和他們的孩子。但讓我們說,用戶選擇一個特定的'ambiente'或特定的'secao',我只能得到一個id或一個節點數據,但要插入該信息,我需要先插入所有父母數據,然後才能將其插入D b。

示例1單個「projeto」選定數據。

[{"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' 選擇,可以具有 'secao' 兒童的或沒有。

[{"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」選定數據。

[{"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並在這裏發佈。

所以我需要的是將選定的節點插入數據庫,但考慮到如果父節點沒有加載在樹上它可能有孩子。當然,當我選擇一個孩子並需要迭代所有備份時,他們會在插入孩子之前插入它的父親(後兩個例子)。

希望你們能幫助我。如果需要澄清,只需要求。

謝謝。

回答

0

好吧,大家半路完成。創建下面的代碼:

//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」數據我需要創建它的父母。在這個例子中,需要插入「projeto」和「uc」,「projeto」,然後插入「uc」,然後纔可以在數據庫中插入「ambiente」。

編輯:好的,這裏是我做了什麼來解決這個問題。 creted以下函數

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

上述檢查的功能,用於它的父存在,然後被插入或創建和它的所有兒童的。當孩子有2或3個級別時,該函數調用它自己並返回父ID。而已。

這是不完整和可怕的醜陋,但爲了澄清的緣故,也許如果有人有同樣的問題,他們可以看到我的想法。