1

I am using CI3 to develop my app. My case is, there is a file with csv extension like this :

 NO,EIR IN,CONT,TYPE,INDEPO,JAM,KODE VSL,VESSEL,VOY,CONSIG,COND IN,CLEAN,TARE,GROSS,KAPASITAS,EX CARGO,LAST AIR),LAST HIDRO,MANU,BUILDER,OWNER

  1,1545053   ,EOLU 1111111,XXXX,21-11-2015,13:00,ABO,ALBERT OLDENDORFF   ,N001   ,ASTABUMI CIPTA      ,DMG,DIRTY,  2400, 20000,  5000,FOOD                ,  -  -    ,  -  -    ,10-11  ,                    

  2,1545052   ,EOLU 1234567,IM04,21-11-2015,10:00,202,WAN HAI 202         ,N 001  ,ANUGERAH AGUNG LUMIN,AVL,DIRTY,  2400, 20000,  1000,MAKANAN             ,  -  -    ,  -  -    ,11-13  ,                    ,APL                 

I have struggling to read it. My controller like this :

$this->load->library('csvreader');

    $data = array('halaman' => 'Data Container',
        'csvData' => $this->csvreader->parse_file(base_url('assets/csv/ZIL.csv')));

    $main_view = $this->load->view('surveyor/v_container', $data, TRUE);
    echo $main_view;

This is the library :

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Csvreader {

var $fields;            /** columns names retrieved after parsing */ 
var $separator = ';';    /** separator used to explode each line */
var $enclosure = '"';    /** enclosure used to decorate each field */

var $max_row_size = 4096;    /** maximum row size to be used for 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 as $row){
        $result[]   =   str_replace('"', '',$row);
    }
    return $result;
   }   
  }
 ?> 

But, when I debug it in view : print_r($csvData), it just give me empty array like this : Array(), the library based from this, Any help it so appreciated.

Community
  • 1
  • 1
Fadly Dzil
  • 2,154
  • 3
  • 34
  • 85
  • Your CSV data does not have the enclosure you specified in your library. – ahmad Nov 28 '15 at 15:49
  • @Ahmad, how to make it enclosure ? Any solution, coz the csv is generated by old app 'CLIPPER' – Fadly Dzil Nov 28 '15 at 15:56
  • just try changing the enclosure to empty string (This should be accessible to you in controller) e.g: ``$this->csvreader->enclosure = '';`` – ahmad Nov 28 '15 at 15:59
  • @Ahmad, Still not working. – Fadly Dzil Nov 29 '15 at 01:47
  • I have tested your example as is on my machine, And it works so there's nothing wrong in your code. unless your CSV file does not look like the one you posted in this question. – ahmad Nov 29 '15 at 15:11

1 Answers1

0

for get csv file data please try this --

if($_FILES["document"]["size"] > 0)
              {
                $file = fopen("upload/bulk_upload/" . $f_newfile1, "r");
                 while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
                 {
                    $lines[] = $emapData[0];
                 }
                fclose($file);
                }

now you can print array like that --

echo "<pre>";
print_r($lines);

please try this way.

Sorav Garg
  • 1,116
  • 1
  • 9
  • 26