从循环codeigniter保存数据

时间:2016-05-06 08:37:44

标签: php codeigniter

我有一个提交循环数据的表单:

<form class="form" id="addForm">
  <input type="hidden" id="request_id" name="request_id" class="form-control" value="<?php echo $request_id; ?>"/>
  <div class="panel-body">
    <?php foreach ($lab_result->result() as $results){?>
      <div class="form-group">
        <div class="col-md-5">
          <input type="hidden" id="request_details_id" name="request_details_id[]" class="form-control" value="<?php echo $results->id; ?>"/>
          <label class="col-sm-12 control-label"><span class="text-warning"><strong>Requested Service &nbsp; : &nbsp; </strong></span> <span class="text-success"><?php echo $results->service_name; ?> - <?php echo $results->service_description; ?></span></label>
          <label class="col-sm-12 control-label"><span class="text-warning"><strong>Service Costs &nbsp; : &nbsp; </strong></span> <span class="text-success">Ksh. <?php echo $results->service_price; ?></span></label>
        </div>
        <div class="col-sm-7">
          <textarea class="form-control" rows="2" id="results" name="results[]" placeholder="Input Lab Results, if none type N/A"></textarea>
        </div>
      </div>
    <?php }?>
  </div><!-- panel-body -->
  <div class="panel-footer">
    <button type="submit" class="btn btn-primary">Send Results</button>
  </div>
</form>

要在编辑中提交表单的Ajax

$.ajax({
    url: '<?php echo base_url(); ?>laboratory/addLabResult',
    type: 'post',
    data: $('#addForm :input'),
    dataType: 'html',   
    success: function(html) {
        $('#addForm')[0].reset();
        bootbox.alert(html, function()
        {
            window.location.reload();
        });
    }
});

保存数据的模型

function save($user){
    $this->db->insert('tbl_patient_treatments', $user);
    return $this->db->insert_id();
}

让控制器陷入困境。

如果我在控制器中打印_r数据,则输出为:

Array (
  [request_id] => 32
  [request_details_id] => Array (
    [0] => 31 [1] => 32
  )
  [results] => Array (
    [0] => None [1] => Fracture
  )
) 

如何将数据保存到表格codeigniter

2 个答案:

答案 0 :(得分:1)

在控制器中添加此代码

$data = array(
    "first_name"=>$this->input->post("account_fname"),
    "last_name"=>$this->input->post("account_lname")
);
$this->Model_name->insert('Table_name',$data);

在模型中添加此代码

public function insert($table,$data){
    $this->db->insert($table,$data);
    $id = $this->db->insert_id();
    return $id;
}

答案 1 :(得分:1)

请用这种方式 -

控制器 -

$data = array(
    "request_id"=>$this->input->post("request_id"),
    "request_details_id"=>serialize($this->input->post("request_details_id")),
    "results"=>serialize($this->input->post("results"))
);
$this->Model_name->insert('Table_name',$data);

型号

public function insert($table,$data){
    $this->db->insert($table,$data);
    $id = $this->db->insert_id();
    return $id;
}

并使用unserialize()函数获取序列化数据,它将转换为php数组