php - How to change the keys/first row in this CSV library import Codeigniter -


class csvreader {      var $fields;            /** columns names retrieved after parsing */      var $separator = ';';    /** separator used explode each line */     var $enclosure = '"';    /** enclosure used decorate each field */      var $max_row_size = 4096;    /** maximum row size used decoding */      function parse_file($p_filepath) {          $file = fopen($p_filepath, 'r');         $this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);         $keys_values = explode(',',$this->fields[0]);          $content    =   array();         $keys   =   $this->escape_string($keys_values);          $i  =   1;         while( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) {                         if( $row != null ) { // skip empty lines                 $values =   explode(',',$row[0]);                 if(count($keys) == count($values)){                     $arr    =   array();                     $new_values =   array();                     $new_values =   $this->escape_string($values);                     for($j=0;$j<count($keys);$j++){                         if($keys[$j] != ""){                             $arr[$keys[$j]] =   $new_values[$j];                         }                     }                      $content[$i]=   $arr;                     $i++;                 }             }         }         fclose($file);         return $content;     }      function escape_string($data){         $result =   array();         foreach($data $row){             $result[]   =   str_replace('"', '',$row);         }         return $result;     }    } 

in code of library importing csv files sql key in first row. want remove first row keys in csv file , add code.

fields in first row of csv want in in code:

$fields = array(        '0'     => 'model',                         '1'     => 'serial_num' ,                         '2'     => 'client_name',                         '3'     => 'end_user',                         '4'     => 'trends_warranty_start',                         '5'     => 'trends_warranty_end',                         '6'     => 'trends_warranty_period',                         '7'     => 'client_contract_no',                         '8'     => 'vendor',                         '9'     => 'support_coverage',                         '10'    => 'sla',                         '11'    => 'service_contract_num',                         '12'    => 'support_coverage_start',                         '13'    => 'support_coverage_end',                         '14'    => 'support_coverage_period',                         '15'    => 'trends_po',                         '16'    => 'supplier_so');  

instead of making change in code can add these columns in first row of csv file. , available in code.

"serial_num" ,"client_name","end_user","trends_warranty_start","trends_warranty_end","trends_warranty_period","client_contract_no","vendor","support_coverage","sla","service_contract_num","support_coverage_start","support_coverage_end","support_coverage_period","trends_po","supplier_so" 

the other possibel scanrio when csv result add column names in result array this

$result =   $this->csvreader->parse_file('test.csv');  $columns    =    array(                     'model',                     'serial_num' ,                     'client_name',                     'end_user',                     'trends_warranty_start',                     'trends_warranty_end',                     'trends_warranty_period',                     'client_contract_no',                     'vendor',                     'support_coverage',                     'sla',                     'service_contract_num',                     'support_coverage_start',                     'support_coverage_end',                     'support_coverage_period',                     'trends_po',                     'supplier_so' );  $resultwithcolumns  =   array_unshift($result , $columns); 

and have column names in result array.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -