how to read csv file in jquery using codeigniter framework

后端 未结 3 1199
爱一瞬间的悲伤
爱一瞬间的悲伤 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:46

    I have tried your CSVReader function. It gives me perfect result as per my need. I also like your idea to check if the CSV fields/headers match to one of your database table fields to insert the row. It gives result array as below.

    Array
    (
        [0] => Array
            (
                [Years] => 1888
                [Make1] => Acura
                [Make2] => Honda
                [Make] => Honda
                [] => Honda
                [Makes] => Toyota
    )
    

    I have also tried CSVReader library at this location. https://github.com/bcit-ci/CodeIgniter/wiki/CSVReader But it gives result array with values imploded by comma(,) as below.

    Array
    (
        [0] => Array
            (
                [Years,Make1,Make2,Make,,Makes,Make] => 1888,Acura,Honda,Toyota,Honda,Toyota,Honda
            )
    )
    

    Now SubRed, in your CSVReader function, I have made changes to CSV file's first row name with whitespace at the end. But after that in the CI controller, while I am checking that fields/headers/column_name exists in CSV file with array_key_exists('column_name',$csvdata), it returns false/none because of whitespace in the column_name field at the end. So I have updated your code.

    Please refer below updated code and give me any suggestions if any.

    EDIT :- Make changes to the foreach loop in parse_lines function in CSVReader file =>

    foreach( $this->fields as $id => $field ) {  
        //echo $id;
        //echo $field;
        //In CSV File, trim(remove both end whitespaces) the first line which contains fields names
        $field = trim($field);
        if( isset($elements[$id]) ) 
        {
            $item[trim($field)] = trim($elements[$id]);//In CSV File, trim all the file data
        }
     }
    

提交回复
热议问题