2017-02-03 16 views
1

我正在動態獲取數組值,但我無法將數組值作爲單獨的行添加到codeigniter中的MySQL數據庫中。將每個數組值插入codeigniter中從控制器到模型的單獨行

從Ajax的輸出是1000乙400

控制器

$myTableArray = json_decode($_POST['myTableArray'], true); 

$appointment_id=$this->user_model->get_details(); 

$details=$this->user_model->patient_investigation_details($myTableArray,$appointment_id); 

模型

function get_details() 
{ 
    $this->db->trans_start(); 
    $query = $this->db->query("SELECT * from `appointment_details` ORDER BY `appointment_id` DESC"); 
    $this->db->trans_complete(); 
    if($query->num_rows()>=1) 
    return $query->result()[0]->appointment_id; 
} 

function patient_investigation_details($myTableArray,$appointment_id) 
{ 
    $query_string="INSERT into `patient_investigation_details`(`investigation_name`,`price`,`appointment_id`) VALUES"; 
    foreach ($myTableArray as $row) 
    { 
    $query_string.="('".$row['1']."','". $row['2']."','".$appointment_id."')"; 
    } 
    $this->db->trans_start(); 
    $query = $this->db->query($query_string); 
    $this->db->trans_complete(); 

    if ($this->db->trans_status() === FALSE){ 
    return null; 
    } 
    else{ 
    return TRUE; 
    } 
} 
+0

請問您是否可以驗證下面的代碼? –

回答

1

你應該嘗試創建用於插入一個數組,然後插入這些批量。這將迭代foreach循環並創建插入數組

也用於得到appointment_id沒有必要從表中拉*(全部)。只需通過應用限制1即可獲得預約ID,即可獲得最頂行

function get_details() 
    { 
     $this->db->trans_start(); 
     $query = $this->db->query("SELECT appointment_id from `appointment_details` ORDER BY `appointment_id` DESC limit 1"); 
     $this->db->trans_complete(); 
     if($query->num_rows()>=1) 
      return $this->db->get()->row()->appointment_id; 
    } 

    function patient_investigation_details($myTableArray,$appointment_id) 
    { 
    foreach ($myTableArray as $row){ 
      $data[] = array(
        'investigation_name' => $row['1'], 
        'price'=> $row['2'], 
        'appointment_id'=>$appointment_id 
      ); 
     } 
    $this->db->insert_batch('patient_investigation_details', $data); 
    } 
+0

謝謝@Nishant奈爾我試過這.....但是當我var_dump我的約會ID我無法得到的ID和它沒有被插入到db – user5370838

+0

嘗試與你'代碼get_details()',然後嘗試插入我的代碼 –

+0

是的,我現在會嘗試 – user5370838

相關問題