suppose this is my csv file
fileempId,lastName,firstName,middleName,street1,street2,city,state,zip,gender,birthDate,ssn,empStatus,joinDate,workStation,locati
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
}
}
Based on your comment that actually you want to upload your CSV file first to your server then check if the CSV fields/headers match to one of your database table fields to insert the row, here the sample code I made:
CSV Reader (based from this library with some adjustments):
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CSVReader Class
*
* $Id: csvreader.php 147 2007-07-09 23:12:45Z Pierre-Jean $
*
* Allows to retrieve a CSV file content as a two dimensional array.
* The first text line shall contains the column names.
*
* @author Pierre-Jean Turpeau
* @link http://www.codeigniter.com/wiki/CSVReader
*/
class CSVReader {
var $fields; /** columns names retrieved after parsing */
var $separator = ','; /** separator used to explode each line */
/**
* Parse a text containing CSV formatted data.
*
* @access public
* @param string
* @return array
*/
function parse_text($p_Text) {
$lines = explode("\n", $p_Text);
return $this->parse_lines($lines);
}
/**
* Parse a file containing CSV formatted data.
*
* @access public
* @param string
* @return array
*/
function parse_file($p_Filepath) {
$lines = file($p_Filepath);
return $this->parse_lines($lines);
}
/**
* Parse an array of text lines containing CSV formatted data.
*
* @access public
* @param array
* @return array
*/
function parse_lines($p_CSVLines) {
$content = FALSE;
foreach( $p_CSVLines as $line_num => $line ) {
if( $line != '' ) { // skip empty lines
$line = trim($line);
$elements = explode($this->separator, $line);
if( !is_array($content) ) { // the first line contains fields names
$this->fields = $elements;
$content = array();
} else {
$item = array();
foreach( $this->fields as $id => $field ) {
if( isset($elements[$id]) ) {
$item[$field] = $elements[$id];
}
}
$content[] = $item;
}
}
}
return $content;
}
/**
* Get fields
*/
public function get_fields(){
return $this->fields;
}
}
Do upload handler on CI:
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('csvfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('csv_upload', $error);
}
else
{
$upload_data = $this->upload->data();
// start to read the CSV file
$this->load->library('csvreader');
$file_path = $upload_data['full_path'];
$csv_data = $this->csvreader->parse_file($file_path);
$csv_fields = $this->csvreader->get_fields();
// list your database table
$tables = $this->db->list_tables();
foreach($tables as $table)
{
$fields = $this->db->list_fields($table);
if($fields == $csv_fields) // match to one of database table
{
// insert the record
foreach($csv_data as $row){
$this->db->insert($table, $row);
}
}
}
$data = array(
'upload_data' => $upload_data,
'csv_data' => $csv_data,
);
$this->load->view('upload_success', $data);
}
}
Download full sample HERE. You can find sample of CSV file inside folder uploads
.
I use this library to convert array to some other data format or vice versa.
CI - Rest Server
There you can find library/class Format (Format.php) that you can use to convert CSV to array then save it into your database. This class also support other format:
EDIT:
This library works on CSV with delimiter "\n" on each row and "," on each of column, you can use it like this:
$this->load->library('format');
$string_csv = "YOUR CSV";
$result = $this->format->factory($string_csv, 'csv')->to_array();
var_dump($result);
Just that simple. However as I said above if your have another delimiter then you have to adjust the library as your need. Here the main function to convert CSV to array:
function _from_csv($string)
{
$data = array();
// Splits
$rows = explode("\n", trim($string));
$headings = explode(',', array_shift($rows));
foreach ($rows as $row)
{
// The substr removes " from start and end
$data_fields = explode('","', trim(substr($row, 1, -1)));
if (count($data_fields) == count($headings))
{
$data[] = array_combine($headings, $data_fields);
}
}
return $data;
}
EDIT 2:
My example will work on this standard CSV format:
Heading1, Heading2, Heading3
"1","John","London"
"2","Brian","Texas"