Friday 4 March 2016

How to import csv file in mysql using codeigniter?

How to import CSV file into MySQL using Codeigniter?

The people have fear about something like new developer if he had not done some task he first thought that can I do it or not. Here I am telling you everything you do consistently and with patience then you will achieve the target. We explain you the things in simplest words so that you can get the idea how it works. Basically, for Codeigniter, I explain the example in three parts for import CSV file in MySQL using Codeigniter framework. Simple and easy way to work within the Codeigniter that is why the lot of developers working in Codeigniter.

1) View
2) Controller
3) Model

how to import csv file in mysql using codeigniter?

STEP ONE

View:

<form action ="" method="post">
 <input type="file" accept=".csv">
</form>

STEP TWO

Controller:

Import CSV library in controller
$this->load->library('csvimport');
Call model function.

STEP THREE

Model:

In model initialize configs and then load the upload library then initialize the config.
$config['upload_path'] = './assets/order_files/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '*';
$this->load->library('upload');
$this->upload->initialize($config);
// If upload failed, display error
if (!$this->upload->do_upload('upload')) {
     $data['error'] = $this->upload->display_errors();        
} else {
$file_data = $this->upload->data(); 
This line will hold all the information about the  file. like name, type, content type etc.
$file_path =  './assets/order_files/'.$file_data['file_name'];
$file_path =  './assets/order_files/'.$file_data['file_name'];
This will hold the file path
if ($this->csvimport->get_array($file_path))
{
    $csv_array = $this->csvimport->get_array($file_path);
    foreach ($csv_array as $row){       $insert_data = array(
        'firstName' => $row['fname'] ,
        'lastName' => $row['lname'],
        'Email' => $row['email'],
}
$q = $this->db->insert('table_Name', $csv_array);
How to import csv file in mysql using codeigniter

That's it. Simple and easy with Codeigniter. You can search with other search queries like import CSV file into MySQL in Codeigniter, Codeigniter CSV import library, CSV upload in Codeigniter, CSV import in Codeigniter example, Codeigniter import CSV file to database, how to import CSV file into database using PHP, Codeigniter export CSV file you get the other sites links for help also. If you have any query you can ask here in a comment box. I will response to your queries. You can further read about the image uploading that how you can upload the image on a server.
How to upload image in PHP...!

This Post Was Last Updated On September 30, 2016, By Author: Mahira Khan.

0 comments:

Post a Comment

Item Reviewed: How to import csv file in mysql using codeigniter? Rating: 5 Reviewed By: admin