以迭代的方式插入到数据库


Insert to db in an iterative way

我想插入到我的数据库支付表,应该以这种方式完成。我拥有的支付表包括group_id和member_id作为分别与表组和成员相关的外键。我想做的是,一旦我点击按钮"支付",它应该插入每个和每个member_id作为支付表中的一行。这是我的代码。在控制器中,我有

public function create_action() 
{
    $this->_rules();
    if ($this->form_validation->run() == FALSE) {
        $this->create();
    } else {
        $this->load->model('Member_model');
        $memberid = $this->Member_model->paymember();
        foreach ($memberid->result_array() as $id){
                $memberid[] = $id;
        $data = array(
    'group_id' => $this->input->post('group_id',TRUE),
    'member_id' => $memberid,
    'paid_by' => $this->input->post('paid_by',TRUE),
    'from_date' => $this->input->post('from_date',TRUE),
    'to_date' => $this->input->post('to_date',TRUE),
    'amount' => $this->input->post('amount',TRUE),
    'reference_number' => $this->input->post('reference_number',TRUE),
    //'last_updated_by' => $this->input->post('last_updated_by',TRUE),
    //'last_update_time' => $this->input->post('last_update_time',TRUE),
    );
        }
        $this->Payment_model->insert($data);
        $this->session->set_flashdata('message', 'Record Created     Successfully!');
        redirect(site_url('payment'));
    }
}

在我的模型中也就是控制器中包含的Member_model。

 function paymember(){
 $data= array();
 $this->db->where('group_id',$this->input->post('group_id'));
 $memberid = $this->db->get('member');
 if ($memberid->num_rows() > 0) {
        foreach ($memberid->result_array() as $row){
                $data[] = $row;
            }
 }
 }

请帮帮我。谢谢你。

现在我解决了这个问题。在我的控制器中,我从模型中获得了数组值。

          public function create_action() 
{
    $this->_rules();
    if ($this->form_validation->run() == FALSE) {
        $this->create();
    } else {
    $this->load->model('Member_model');
    $memberid = $this->Member_model->paymember();
        if (count($memberid)) {
            foreach ($memberid as $id) {
            $data = array(
        'group_id' => $this->input->post('group_id',TRUE),
        'member_id' => $id,
        'paid_by' => $this->input->post('paid_by',TRUE),
        'from_date' => $this->input->post('from_date',TRUE),
        'to_date' => $this->input->post('to_date',TRUE),
        'amount' => $this->input->post('amount',TRUE),
        'reference_number' => $this->input->post('reference_number',TRUE),
        'last_updated_by' => $this->session->userdata('username'),
        //'last_update_time' => $this->input->post('last_update_time',TRUE),
        );
           $this->Payment_model->insert($data);
           echo $data;
        }
    }
        $this->session->set_flashdata('message', 'Record Created Successfully!');
        redirect(site_url('payment'));
    }
}

在我之前的模型paymember方法中也有一个问题,它没有返回任何结果所以我用这个代码....

修复了这个问题
        function paymember(){
$data= array();
$this->db->where('group_id',$this->input->post('group_id'));
$query = $this->db->get('member');
    if ($query->num_rows() > 0) {
        foreach ($query->result_array() as $row){
                $data[] = $row['member_id'];
            }
    }
    $query->free_result();
    return $data;

}

感谢您的支持!