从下拉列表中选择的值将与Excel工作表数据一起插入到数据库中

时间:2013-12-22 17:27:08

标签: php codeigniter

在下面的代码我已经放置了控制器,模型和视图。其中我得到3列nim,nama,alamat从excel和考试名称从下拉列表插入到db.Now我下了一个下拉选择的考试从要插入db.Pls的考试课程表中的名称帮助我解决问题。 控制器:

public function index()

    {   
//echo "inside form upload";   

        $data = array();
        //$college_name = $this->session->userdata('college_name');
        if($query = $this->import_model->get_exam_data())
        {
            $data['exam_data'] = $query;
        }
        //$this->load->view('student_view', $data);

        $this->load->view('form_upload',$data);
    }
public function upload()
    {
        $this->load->helper('file');
          //echo "inside after load helper </br>";      
       $config['upload_path'] = './upload/';

        //echo  $config['upload_path']  ;
        // echo "inside after upload path </br>"; 
        $config['allowed_types'] = 'xls';
        //echo "allowed_types  </br>";

        $this->load->library('upload', $config);

    $result=$this->upload->do_upload('file');
         //echo $result;

        if ( $this->upload->do_upload('file'))
        {

        //echo "inside else upload </br>";
            $data = array('error' => false);
            $upload = $this->upload->data();

            $this->load->library('excel_reader');
            $this->excel_reader->setOutputEncoding('CP1251');

            $file = $upload['full_path'];
            $this->excel_reader->read($file);

            $data      = $this->excel_reader->sheets[0];
            $excel_data = Array();
            for ($i = 1; $i <= $data['numRows']; $i++)
            {
                if($data['cells'][$i][1] == '') break;
                $excel_data[$i-1]['nim']    = $data['cells'][$i][1];
                $excel_data[$i-1]['nama']   = $data['cells'][$i][2];
                $excel_data[$i-1]['alamat'] = $data['cells'][$i][3];                
            }            
            delete_files($upload['file_path']);
            $this->import_model->upload_data($excel_data);            
            redirect('index.php/success/');
        }
    }
}

模型:

function get_exam_data()
    {
        $this->db->distinct();
        $this->db->select("CONCAT(exam_name) AS fullexamname", FALSE);//this will concat the value
        //$this->db->where('college_name',$college_name);
        $query = $this->db->get('examcourse');
         $this->db->insert('mahasiswa', $data);
        return $query->result();
    }
public function upload_data($excel_data)
    {       
        for($i = 1; $i < count($excel_data); $i++)
        {            
            if( ! $this->is_exist($excel_data[$i]['nim']) == TRUE)
            {
                $data = array(
                        'nim'    => $excel_data[$i]['nim'],
                        'nama'   => $excel_data[$i]['nama'],
                        'alamat' => $excel_data[$i]['alamat']                        
                        );
                $this->db->insert('mahasiswa', $data);
            }
            else
            {
                $data = array(                   
                        'nama'   => $excel_data[$i]['nama'],
                        'alamat' => $excel_data[$i]['alamat']
                        );
                $this->db->where('nim', $excel_data[$i]['nim']);
                $this->db->update('mahasiswa', $data);
            }
        }

视图:

<div id="body">
  <form action="http://localhost/CodeIgniter/index.php/import/upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">        <?php 
        $data = array();
        $data["Select Exam Name"] = "Select Exam Name"; 
        foreach ($exam_data as $row)
        {
            $data[$row->fullexamname] = $row->fullexamname; 
        }
        echo form_dropdown('exam_name', $data, 'small', 'class="dropdown_class"  id="exam_name_id" onChange="get_subjectcodedetails()"');
?>
<div id="ssubject_code">

  <label for="file">Browse File</label>        <br>

        <input type="file" name="file" value="Browse File">        <br>
        <input type="submit" name="button" value="Import">        
        </form> </div>
        }

0 个答案:

没有答案