在codeigniter中创建导出和导入我需要一个助手

Creating export and import in codeigniter i need an assistant

本文关键字:一个 创建 codeigniter 导入      更新时间:2023-09-26

我需要一个助手在codeigniter中创建导出和导入文件,我在编码方面并不先进,我还在学习。我没有添加导出按钮,因为我还需要一个助手来添加

这是我的看法

<form method="post" action="<?php echo base_url() ?>csv/importcsv" enctype="multipart/form-data">
    <input type="file" name="userfile" size="10" style="width:20px;">
    <input type="submit" name="submit" value="IMPORT" class="btn btn-primary">
</form>

这是我的控制器

/****MANAGE MEMBERS*****/
function member($param1 = '', $param2 = '', $param3 = '') {
    if ($this->session->userdata('admin_login') != 1)
        redirect(base_url(), 'refresh');
    if ($param1 == 'create') {
        $data['names']                          =   $this->input->post('names');
        $data['surname']                        =   $this->input->post('surname');
        $data['title']                          =   $this->input->post('title');
        $data['initials']                       =   $this->input->post('initials');
        $data['id_number']                      =   $this->input->post('id_number');
        $data['passport']                       =   $this->input->post('passport');
        $data['birthdate']                      =   $this->input->post('birthdate');
        $data['gender']                         =   $this->input->post('gender');
        $data['email']                          =   $this->input->post('email');
        $data['address_1']                      =   $this->input->post('address_1');
        $data['address_2']                      =   $this->input->post('address_2');
        $data['address_3']                      =   $this->input->post('address_3');
        $data['province']                       =   $this->input->post('province');
        $data['country']                        =   $this->input->post('country');
        $data['postal_code']                    =   $this->input->post('postal_code');
        $data['phone']                          =   $this->input->post('phone');
        $data['tel']                            =   $this->input->post('tel');
        $data['country']                        =   $this->input->post('country');
        $data['region']                         =   $this->input->post('region');
        $data['branch']                         =   $this->input->post('branch');
        $data['creation_timestamp']             =   $this->input->post('creation_timestamp');
        $data['expiry_timestamp']               =   $this->input->post('expiry_timestamp');
        $data['beneficiary_name']               =   $this->input->post('beneficiary_name');
        $data['beneficiary_surname']            =   $this->input->post('beneficiary_surname');
        $data['beneficiary_id_number']          =   $this->input->post('beneficiary_id_number');
        $data['beneficiary_phone']              =   $this->input->post('beneficiary_phone');
        $data['message']                        =   $this->input->post('message');
        $data['chat_status']                    =   $this->input->post('chat_status');
        $this->db->insert('member', $data);
        $member_id = mysql_insert_id();
        move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/member_image/' . $member_id . '.jpg');
        $this->email_model->account_opening_email('member', $data['email']); //SEND EMAIL ACCOUNT OPENING EMAIL
        redirect(base_url() . 'index.php?admin/member/', 'refresh');
    }
    if ($param1 == 'do_update') {
        $data['names']                          =   $this->input->post('names');
        $data['surname']                        =   $this->input->post('surname');
        $data['title']                          =   $this->input->post('title');
        $data['initials']                       =   $this->input->post('initials');
        $data['id_number']                      =   $this->input->post('id_number');
        $data['passport']                       =   $this->input->post('passport');
        $data['birthdate']                      =   $this->input->post('birthdate');
        $data['gender']                         =   $this->input->post('gender');
        $data['email']                          =   $this->input->post('email');
        $data['address_1']                      =   $this->input->post('address_1');
        $data['address_2']                      =   $this->input->post('address_2');
        $data['address_3']                      =   $this->input->post('address_3');
        $data['province']                       =   $this->input->post('province');
        $data['country']                        =   $this->input->post('country');
        $data['postal_code']                    =   $this->input->post('postal_code');
        $data['phone']                          =   $this->input->post('phone');
        $data['tel']                            =   $this->input->post('tel');
        $data['country']                        =   $this->input->post('country');
        $data['region']                         =   $this->input->post('region');
        $data['branch']                         =   $this->input->post('branch');
        $data['creation_timestamp']             =   $this->input->post('creation_timestamp');
        $data['expiry_timestamp']               =   $this->input->post('expiry_timestamp');
        $data['beneficiary_name']               =   $this->input->post('beneficiary_name');
        $data['beneficiary_surname']            =   $this->input->post('beneficiary_surname');
        $data['beneficiary_id_number']          =   $this->input->post('beneficiary_id_number');
        $data['beneficiary_phone']              =   $this->input->post('beneficiary_phone');
        $data['message']                        =   $this->input->post('message');
        $data['chat_status']                    =   $this->input->post('chat_status');
        $this->db->where('member_id', $param2);
        $this->db->update('member', $data);
        move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/member_image/' . $param2 . '.jpg');
        redirect(base_url() . 'index.php?admin/member/', 'refresh');
    } else if ($param1 == 'personal_profile') {
        $page_data['personal_profile']   = true;
        $page_data['current_member_id'] = $param2;
    } else if ($param1 == 'edit') {
        $page_data['edit_data'] = $this->db->get_where('member', array(
                        'member_id' => $param2
                    ))->result_array();
    }
    if ($param1 == 'delete') {
        $this->db->where('member_id', $param2);
        $this->db->delete('member');
        redirect(base_url() . 'index.php?admin/member/', 'refresh');
    }
    $page_data['teachers']   = $this->db->get('member')->result_array();
    $page_data['page_name']  = 'member';
    $page_data['page_title'] = get_phrase('manage_member');
    $this->load->view('index', $page_data);
    if (!this->load->importcsv()); {
        $data['memberbook'] = $this->csv_model->get_memberbook();
        $data['error'] = '';    //initialize image upload error array to empty
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'csv';
        $config['max_size'] = '1000';
        $this->load->library('upload', $config);
        // If upload failed, display error
        if (!$this->upload->do_upload()) {
            $data['error'] = $this->upload->display_errors();
            $this->load->view('index', $data);
        } else {
            $file_data = $this->upload->data();
            $file_path =  './uploads/'.$file_data['file_name'];
            if ($this->csvimport->get_array($file_path)) {
                $csv_array = $this->csvimport->get_array($file_path);
                foreach ($csv_array as $row) {
                    $insert_data = array(
                                'names'=>$row['names'],
                                'surname'=>$row['surname'],
                                'initials'=>$row['initials'],
                                'id_number'=>$row['id_number'],
                                'passport'=>$row['passport'],
                                'birthdate'=>$row['birthdate'],
                                'gender'=>$row['gender'],
                                'phone'=>$row['phone'],
                                'email'=>$row['email'],
                                'address_1'=>$row['address_2'],
                                'address_2'=>$row['address_2'],
                                'address_3'=>$row['address_3'],
                                'province'=>$row['province'],
                                'country'=>$row['country'],
                                'postal_code'=>$row['postal_code'],
                                'tel'=>$row['tel'],
                                'region'=>$row['region'],
                                'branch'=>$row['branch'],
                                'creation_timestamp'=>$row['creation_timestamp'],
                                'expiry_timestamp'=>$row['expiry_timestamp'],
                                'beneficiary_name'=>$row['beneficiary_name'],
                                'beneficiary_surname'=>$row['beneficiary_surname'],
                                'beneficiary_id_number'=>$row['beneficiary_id_number'],
                                'beneficiary_phone'=>$row['beneficiary_phone'],
                                'message'=>$row['message'], 
                            );
                $this->csv_model->insert_csv($insert_data);
            }
            $this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
            redirect(base_url().'csv');
            //echo "<pre>"; print_r($insert_data);
        } else 
            $data['error'] = "Error occured";
            $this->load->view('csvindex', $data);
        }                       
    }       
}

在解决这个问题几个月后,我想出了一个解决方案,所以我想我应该把它分享给一些有同样困难的人

控制器

            function member_bulk_add($param1 = '')
            {
                if ($this->session->userdata('admin_login') != 1)
                    redirect(base_url(), 'refresh');
                if ($param1 == 'import_excel')
                {
                    move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/import.xlsx');
                    // Importing excel sheet for bulk student uploads
                    include 'simplexlsx.php';
                    $xlsx = new SimpleXLSX('assets/import.xlsx');
                    list($num_cols, $num_rows) = $xlsx->dimension();
                    $f = 0;
                    foreach( $xlsx->rows() as $r ) 
                    {
                        // Ignore the inital name row of excel file
                        if ($f == 0)
                        {
                            $f++;
                            continue;
                        }
                        for( $i=0; $i < $num_cols; $i++ )
                        {
                            if ($i == 0)    $data['name']                   =   $r[$i];
                            else if ($i == 1)   $data['birthday']       =   $r[$i];
                            else if ($i == 2)   $data['sex']                =   $r[$i];
                            else if ($i == 3)   $data['address']            =   $r[$i];
                            else if ($i == 4)   $data['phone']          =   $r[$i];
                            else if ($i == 5)   $data['email']          =   $r[$i];
                        }
                        $data['member_id']  =   $this->input->post('member_id');
                        $this->db->insert('member' , $data);
                        //print_r($data);
                    }
                    redirect(base_url() . 'index.php?admin/member/' . $this->input->post('member_id'), 'refresh');
                $this->load->view('backend/index', $page_data);
            }