以迭代方式插入数据库

时间:2016-09-08 18:22:55

标签: php codeigniter

我想插入应该以这种方式完成的数据库付款表。 我拥有的支付表包括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;
            }
 }
 }

请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:0)

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

          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;

}

感谢您的支持!