get_param(); //mitra foreach ($payload["mitra"] as $m) { $status = $this->insert_or_update("one_mitra.mitra", $m, [ "M_BranchCode", "MitraID", ]); print_r($status); } } function insert_or_update($table, $dt, $keys) { $s_where = ""; $param = []; foreach ($keys as $k) { if ($s_where != "") { $s_where .= " and "; } $s_where .= " $k = ?"; $param[] = $dt[$k]; } $sql = "select count(*) as total from $table where $s_where "; $qry = $this->db->query($sql, $param); if (!$qry) { return [ "status" => "ERR", "message" => $this->db->error()["message"] . "|" . $this->db->last_query(), ]; } $rows = $qry->result_array(); $status = "Insert"; if (count($rows) > 0) { if ($rows[0]["total"] > 0) { foreach ($keys as $k) { $this->db->where($k, $dt[$k]); } $qry = $this->db->update($table, $dt); if (!$qry) { return [ "status" => "ERR", "message" => "ERR Update : " . $this->db->error()["message"] . "|" . $this->db->last_query(), ]; } $status = "Update"; } else { //insert $qry = $this->db->insert($table, $dt); if (!$qry) { return [ "status" => "ERR", "message" => "ERR Insert : " . $this->db->error()["message"] . "|" . $this->db->last_query(), ]; } } } else { //insert $qry = $this->db->insert($table, $dt); if (!$qry) { return [ "status" => "ERR", "message" => "ERR Insert : " . $this->db->error()["message"] . "|" . $this->db->last_query(), ]; } } return ["status" => "OK", "message" => $status]; } function get_param() { $zbody = file_get_contents("php://input"); $body = gzuncompress($zbody); $jbody = json_decode($body, true); $r_md5 = $jbody["md5"]; $l_md5 = md5(json_encode($jbody["payload"])); if ($r_md5 != $l_md5) { $this->reply_err("MD5 Invalid $r_md5 <> $l_md5"); } return $jbody["payload"]; } function reply($data = []) { echo json_encode([ "status" => "OK", "data" => $data, ]); exit(); } function reply_err($message = "") { echo json_encode([ "status" => "ERR", "message" => $message, ]); exit(); } }