To import data from csv to mysql we need to convert excel file data to csv file and then upload using front end page using php codeigniter framework.
Now we will move to coding part ,Here we need to create three files in codeigniter folders i.e
1) Model file
2) View file
3) Controller file.
Now In the first step we will create View file as Upload_csv.php under View folder.


<form action="user/upload_sampledata" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
<table>
<tr>
<td> Choose your file: </td>
<td><input type="file" class="form-control" name="userfile" id="userfile"  align="center"/>
</td>
<td><div class="col-lg-offset-3 col-lg-9">    <button type="submit" name="submit" class="btn btn-info"  >   Save  </button>
</div></td></tr></table> 
</form>

Now in the second step we will create controller file as User.php under controllers folder.
Here we are uploading the csv file using upload_sampledata function.

<?php
class User extends CI_Controller{
public $data;
public function __construct()
{
//Core controller constructor
parent::__construct();

$this->load->model('upload_services');
$this->load->library('form_validation');
    
}
function upload_sampledata()
{
$data['result']=$this->upload_services->upload_sampledata_csv();
$data['query']=$this-> upload_services->get_car_features_info();
$this->load->view(' Upload_csv ',$data);
}
}

Now in the third step we will create Model file as upload_services.php under Models folder.


[sociallocker]

<?php
class Upload_services extends CI_Model
 {
function __construct()
{
				
parent::__construct();
				
		 
}
 function upload_sampledata_csv()
 {
		   
  $fp = fopen($_FILES['userfile']['tmp_name'],'r') or die("can't open file");
					  while($csv_line = fgetcsv($fp,1024)) 
					  {
					for ($i = 0, $j = count($csv_line); $i < $j; $i++) 
					  {
			
					
					   $insert_csv = array();
					   $insert_csv['brand'] = $csv_line[0];
					   $insert_csv['dealer_name'] = $csv_line[1];
					   $insert_csv['authorized'] = $csv_line[2];
					   $insert_csv['address'] = $csv_line[3];
					   $insert_csv['contact_no'] = $csv_line[4];
					   $insert_csv['mobile_no'] = $csv_line[5];
					   $insert_csv['fax'] = $csv_line[6];
					   $insert_csv['email_id'] = $csv_line[7];
					   $insert_csv['website_addr'] = $csv_line[8];
					   $insert_csv['state'] = $csv_line[9];
					   $insert_csv['city'] = $csv_line[10];
					  
					  }
	 
					  $data = array(
					   'brand' => $insert_csv['brand'] ,
					   'dealer_name' => $insert_csv['dealer_name'],
					   'authorized' => $insert_csv['authorized'],
					   'address' => $insert_csv['address'] ,
					   'contact_no' => $insert_csv['contact_no'],
					   'mobile_no' => $insert_csv['mobile_no'],
					   'fax' => $insert_csv['fax'] ,
					   'email_id' => $insert_csv['email_id'] ,
					   'website_addr' => $insert_csv['website_addr'],
					   'state' => $insert_csv['state'],
					   'city' => $insert_csv['city']);
	      $data['crane_features']=$this->db->insert('sampledata', $data);
				     }
                   fclose($fp) or die("can't close file");
	       $data['success']="success";
	       return $data;
	
	          }
function get_car_features_info()
		   {
		      $get_cardetails=$this->db->query("select * from sampledata");
			   return $get_cardetails;
		   
		   }
}

[/sociallocker]
In the above code i used fgetcsv($fp,1024) to get csv line count and stored in $csv_line variable. Now using for loop read the csv file data and storing in an array and passing to codeigniter default insert function as shown above.
To maintain good website for online money making ,just take good Virtual Private Servers, Built for Open Source Developers , Fully managed, lightning-fast, starting at $15/mo with 1GB RAM! , a developer running a complex application that requires instant scalability, or a designer wanting to give your clients’ sites maximum performance, then VPS hosting is just what you need!
Thank you for reading this article.