在codeigniter中获取多个客户详细信息

时间:2018-09-14 05:58:17

标签: php codeigniter-3

我已经编写了这段代码来从Android设备接收数据。它仅插入了一个客户数据,如果应用程序离线,我需要接收多个客户详细信息。但是它也在离线模式下将一个数据插入数据库。我该如何为多个客户数据插入更改此数据。

function index_post($customerID = false) {
    if ($customerID) {
        //update the record
        $updateData = array();
        $allowedparams = array('streetid' => 'streetid', 'name' => 'name', 'mobile' => 'mobile', 'adhaar' => 'adhaar', 'profession' => 'profession', 'address' => 'address', 'pincode' => 'pincode', 'nearby' => 'nearby', 'paddress' => 'paddress', 'isOwned' => 'isOwned');
        foreach ($allowedparams as $k => $v) {
            if (!$this->IsNullOrEmptyString($this->post($k, true))) {
                $updateData[$v] = $this->post($k, true);
            }
        }
        if ($this->model_customer->update($customerID, $updateData)) {
            $data = array('status' => true, 'messsage' => 'cusotmer updated succesfully');
            $http_code = REST_Controller::HTTP_OK;
        } else {
            $data = array('status' => false, 'messsage' => 'cusotmer failed to update.');
            $http_code = REST_Controller::HTTP_INTERNAL_SERVER_ERROR;
        }
    } else {
        //insert the record
        $allowedparams = array('streetid' => 'streetid', 'name' => 'name', 'mobile' => 'mobile', 'adhaar' => 'adhaar', 'profession' => 'profession', 'address' => 'address', 'pincode' => 'pincode', 'cycle' => 'cycle', 'nearby' => 'nearby', 'paddress' => 'paddress', 'isOwned' => 'isOwned');
        $requiredParam = array('streetid', 'name', 'mobile', 'cycle');
        $insertdata = array();
        foreach ($allowedparams as $k => $v) {
            if (in_array($k, $requiredParam)) {
                //check if its not null
                if ($this->post($k) == null || trim($this->post($k)) == '') {
                    $data = array('status' => false, 'message' => $k . ' parameter missing or empty');
                    $http_code = REST_Controller::HTTP_BAD_REQUEST;
                    break;
                }
            }
            $insertData[$v] = $this->post($k, true);
        }
        if ($customerID = $this->model_customer->create($insertData)) {

            $data['customerID'] = $this->_frameCustomer2($this->model_customer->get($customerID));  //you need to put
            $http_code = REST_Controller::HTTP_OK;
        } else {
            $data = array('status' => false, 'message' => 'unable to create customer');
            $http_code = REST_Controller::HTTP_INTERNAL_SERVER_ERROR;
        }
    }
    $this->response($data, $http_code);
}
private function _frameCustomer2($c) {                       //get value from index_get
    $data = array();
    $data['id'] = $c->id;
    $data['name'] = $c->name;
    $data['street'] = array('id' => $c->streetid);
    $data['creationDate'] = $c->creationdate;
    $data['mobile'] = $c->mobile;
    $data['adhaar'] = $c->adhaar;
    $data['profession'] = $c->profession;
    $data['isOwned'] = ($c->isOwned == 1) ? true : false;
    $data['address'] = $c->address;
    $data['pincode'] = $c->pincode;
    $data['status'] = $c->status;
    $data['cycle'] = $c->cycle;
    $data['balance'] = $c->balance;
    $data['creditAvailable'] = $c->creditbalance;
    $data['nearby'] = $c->nearby;
    $data['accountNumber'] = $c->accountnumber;
    $data['permanentAddress'] = $c->paddress;
    $data['lastVisit'] = $this->model_customer->lastVisit($c->id);
    return $data;
}

我的模型功能部分是

function create($insertdata = array()) {                             //new customer insert
    if ($this->db->insert('customer', $insertdata)) {
        return $this->db->insert_id();
    } else {
        return false;
    }
}

function update($customerID = 0, $updateData = array()) {
    $this->db->where('id', $customerID);
    if ($this->db->update('customer', $updateData) && $this->db->affected_rows() == 1) {
        return true;
    } else {
        return false;
    }

0 个答案:

没有答案