Need to implement csv or xls import into Application created using CodeIgniter. Is there any library for this? Any suggestion appreciated.
Here is an easy way to do this. I don't know what people do but i use this
This is my 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_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 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 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>
Reference Here
This is an improved version of raheel shan's accepted answer - I edited his answer but my edit was rejected, however it is an important change...
When parsing each data row, it is not wise to use explode()
on the comma as this does not handle quote-encased strings that contain commas. Explode breaks those strings into substrings and gives extra array elements in $values
, so this check fails:
if (count($keys) == count($values)) {
Instead, PHP has a purpose-built method to handle this; str_getcsv(). I have updated the original answer code accordingly.
The 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>
Well don't mind me, I just modified ajmedway's library to include delimiters just in case you wanted to get data from lets say TSV or pipes delimited files. If you want plain old CSV his is just 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;
}}?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With