how to read csv file in jquery using codeigniter framework

后端 未结 3 1201
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 10:16

suppose this is my csv file

fileempId,lastName,firstName,middleName,street1,street2,city,state,zip,gender,birthDate,ssn,empStatus,joinDate,workStation,locati         


        
3条回答
  •  天涯浪人
    2021-01-07 10:54

    Based on your comment that actually you want to upload your CSV file first to your server then check if the CSV fields/headers match to one of your database table fields to insert the row, here the sample code I made:

    CSV Reader (based from this library with some adjustments):

    parse_lines($lines);
        }
    
        /**
         * Parse a file containing CSV formatted data.
         *
         * @access    public
         * @param    string
         * @return    array
         */
        function parse_file($p_Filepath) {
            $lines = file($p_Filepath);
            return $this->parse_lines($lines);
        }
        /**
         * Parse an array of text lines containing CSV formatted data.
         *
         * @access    public
         * @param    array
         * @return    array
         */
        function parse_lines($p_CSVLines) {    
            $content = FALSE;
            foreach( $p_CSVLines as $line_num => $line ) {                        
                if( $line != '' ) { // skip empty lines
                    $line = trim($line);
    
                    $elements = explode($this->separator, $line);
    
                    if( !is_array($content) ) { // the first line contains fields names
                        $this->fields = $elements;
                        $content = array();
                    } else {
                        $item = array();
                        foreach( $this->fields as $id => $field ) {
                            if( isset($elements[$id]) ) {
                                $item[$field] = $elements[$id];
                            }
                        }
                        $content[] = $item;
                    }
                }
            }
            return $content;
        }
    
        /**
         * Get fields
         */
        public function get_fields(){
            return $this->fields;
        }
    }
    

    Do upload handler on CI:

    public function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'csv';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
    
        $this->load->library('upload', $config);
    
        if ( ! $this->upload->do_upload('csvfile'))
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('csv_upload', $error);
        }
        else
        {
            $upload_data = $this->upload->data();
    
            // start to read the CSV file
            $this->load->library('csvreader');
            $file_path = $upload_data['full_path'];
    
            $csv_data = $this->csvreader->parse_file($file_path); 
            $csv_fields = $this->csvreader->get_fields();
    
            // list your database table
            $tables = $this->db->list_tables();
            foreach($tables as $table)
            {
                $fields = $this->db->list_fields($table);
    
                if($fields == $csv_fields) // match to one of database table
                {
                    // insert the record
                    foreach($csv_data as $row){                        
                        $this->db->insert($table, $row);
                    }
                }
            }
    
            $data = array(
                'upload_data' => $upload_data,
                'csv_data' => $csv_data,
            );
    
            $this->load->view('upload_success', $data);
        }
    }
    

    Download full sample HERE. You can find sample of CSV file inside folder uploads.

提交回复
热议问题