CSV Import Library for CodeIgniter

You must implement csv or xls import in an application created using CodeIgniter. Is there a library for this? Any suggestion appreciated.

+7
source share
5 answers

Here is an easy way to do this. I don’t know what people do, but I use it

This is my csv reading 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; } } ?> 

And the controller method

 function readExcel() { $this->load->library('csvreader'); $result = $this->csvreader->parse_file('Test.csv'); $data['csvData'] = $result; $this->load->view('view_csv', $data); } 

And this view

 <table cellpadding="0" cellspacing="0" width="100%"> <tr> <td width = "10%">ID</td> <td width = "20%">NAME</td> <td width = "20%">SHORT DESCRIPTION</td> <td width = "30%">LONG DESCRIPTION</td> <td width = "10%">STATUS</td> <td width = "10%">PARENTID</td> </tr> <?php foreach($csvData as $field){?> <tr> <td><?php echo $field['id']?></td> <td><?php echo $field['name']?></td> <td><?php echo $field['shortdesc']?></td> <td><?php echo $field['longdesc']?></td> <td><?php echo $field['status']?></td> <td><?php echo $field['parentid']?></td> </tr> <?php }?> </table> 

Link Here

+19
source

This is an improved version of Rahel Shann's accepted answer - I edited his answer, but my editing was rejected, however this is an important change ...

When analyzing each row of data, it is impractical to use explode() in a comma, since this does not process explode() strings containing commas. Explode breaks these lines into substrings and provides additional array elements in $values , so this check is not performed:

 if (count($keys) == count($values)) { 

Instead, PHP has a specially designed method for this; str_getcsv () . I updated the response source code accordingly.

CSV Reader 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 = str_getcsv($this->fields[0]); $i = 1; while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) { if ($row != null) { // skip empty lines $values = str_getcsv($row[0]); if (count($keys) == count($values)) { $arr = array(); for ($j = 0; $j < count($keys); $j++) { if ($keys[$j] != "") { $arr[$keys[$j]] = $values[$j]; } } $content[$i] = $arr; $i++; } } } fclose($file); return $content; } } ?> 

Controller Method:

 function readExcel() { $this->load->library('csvreader'); $result = $this->csvreader->parse_file('Test.csv'); $data['csvData'] = $result; $this->load->view('view_csv', $data); } 

And this is the view:

 <table cellpadding="0" cellspacing="0" width="100%"> <tr> <td width="10%">ID</td> <td width="20%">NAME</td> <td width="20%">SHORT DESCRIPTION</td> <td width="30%">LONG DESCRIPTION</td> <td width="10%">STATUS</td> <td width="10%">PARENTID</td> </tr> <?php foreach ($csvData as $field) { ?> <tr> <td><?php echo $field['id'] ?></td> <td><?php echo $field['name'] ?></td> <td><?php echo $field['shortdesc'] ?></td> <td><?php echo $field['longdesc'] ?></td> <td><?php echo $field['status'] ?></td> <td><?php echo $field['parentid'] ?></td> </tr> <?php } ?> </table> 
+3
source

Well, not against me, I just changed the ajmedway library to include delimiters in case you want to receive data to allow the use of TSV delimited files or pipes. If you need a plain old CSV, everything is fine.

 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, $delimiter = FALSE ) { $file = fopen($p_Filepath, 'r'); $this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure); if ($delimiter==FALSE) { $keys = str_getcsv($this->fields[0]); $i = 1; while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) { if ($row != null) { // skip empty lines $values = str_getcsv($row[0]); if (count($keys) == count($values)) { $arr = array(); for ($j = 0; $j < count($keys); $j++) { if ($keys[$j] != "") { $arr[$keys[$j]] = $values[$j]; } } $content[$i] = $arr; $i++; } } } } else{ $keys = str_getcsv($this->fields[0],$delimiter); $i = 1; while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) { if ($row != null) { // skip empty lines $values = str_getcsv($row[0],$delimiter); if (count($keys) == count($values)) { $arr = array(); for ($j = 0; $j < count($keys); $j++) { if ($keys[$j] != "") { $arr[$keys[$j]] = $values[$j]; } } $content[$i] = $arr; $i++; } } } } fclose($file); return $content; }}?> 
+2
source

This is an improved version for blank lines and extra spaces and tabs ...

class CSVReader {

 function csv_to_array($Filepath) { //Get csv file content $csvData = file_get_contents($Filepath); //Remove empty lines $csvData = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\r\n", $csvData); //String convert in array formate and remove double quote(") $array = array(); $array = $this->escape_string(preg_split('/\r\n|\r|\n/', $csvData)); $new_content_in_array = array(); if($array) { //Get array key $array_keys = array(); $array_keys = array_filter(array_map('trim', explode(';',$array[0]))); //Get array value $array_values = array(); for ($i=1;$i<count($array);$i++) { if($array[$i]) { $array_values[] = array_filter(array_map('trim', explode(';',$array[$i]))); } } //Convert in associative array if($array_keys && $array_values) { $assoc_array = array(); foreach ($array_values as $ky => $val) { for($j=0;$j<count($array_keys);$j++){ if($array_keys[$j] != "" && $val[$j] != "" && (count($array_keys) == count($val))) { $assoc_array[$array_keys[$j]] = $val[$j]; } } $new_content_in_array[] = $assoc_array; } } } return $new_content_in_array; } function escape_string($data){ $result = array(); foreach($data as $row){ $result[] = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", str_replace('"', '',$row)); } return $result; } 

}? >

0
source

Controller call:

 $this->load->library('csvreader'); $import_csv_data = $this->csvreader->csv_to_array($path); print_r($import_csv_data ); exit; 
-one
source

All Articles