How to Import and Export CSV Files Using Codeigniter and MySQL

How to Import and Export CSV Files Using Codeigniter and MySQL

How to Import and Export CSV Files Using Codeigniter and MySQL

In this Post We Will Explain About is How to Import and Export CSV Files Using Codeigniter and MySQL With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Import CSV Data into Mysql in Codeigniter Example

In this post we will show you Best way to implement php – How do I import CSV to mysql using codeigniter, hear for How To Import CSV Data Into Mysql Using Codeigniter with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Read Also:  PHP Object Oriented Programming Tutorial Example

controllers – Ci_csvImp.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ci_csvImp extends CI_Controller {
 
 public function __construct()
 {
  parent::__construct();
  $this->load->model('csv_import_ci_model');
  $this->load->library('csvimport');
 }

 function index()
 {
  $this->load->view('csv_import');
 }

 function load_data()
 {
  $result = $this->csv_import_ci_model->select();
  $output = '
   <h3 align="center">Imported Student Information from CSV File</h3>
        <div class="table-responsive">
         <table class="table table-bordered table-striped">
          <tr>
           <th>Sr. No</th>
           <th>Student First Name</th>
           <th>Student Last Name</th>
           <th>Student Mobile</th>
           <th>Student Email</th>
          </tr>
  ';
  $count = 0;
  if($result->num_rows() > 0)
  {
   foreach($result->result() as $row)
   {
    $count = $count + 1;
    $output .= '
    <tr>
     <td>'.$count.'</td>
     <td>'.$row->studfname.'</td>
     <td>'.$row->studlname.'</td>
     <td>'.$row->stud_mobile.'</td>
     <td>'.$row->email.'</td>
    </tr>
    ';
   }
  }
  else
  {
   $output .= '
   <tr>
       <td colspan="5" align="center">any student Data not Available</td>
      </tr>
   ';
  }
  $output .= '</table></div>';
  echo $output;
 }

 function import()
 {
  $file_data = $this->csvimport->get_array($_FILES["csv_file"]["tmp_name"]);
  foreach($file_data as $row)
  {
   $data[] = array(
    'studfname' => $row["First Name"],
          'studlname'  => $row["Last Name"],
          'stud_mobile'   => $row["Phone"],
          'email'   => $row["Email"]
   );
  }
  $this->csv_import_ci_model->insert($data);
 }
}

models – Ci_csvImp_model.php

<?php
class Ci_csvImp_model extends CI_Model
{
 function select()
 {
  $this->db->order_by('id', 'DESC');
  $query = $this->db->get('student_list');
  return $query;
 }

 function insert($data)
 {
  $this->db->insert_batch('student_list', $data);
 }
}

views – csv_import.php

<html>
<head>
<title></title>
<script src="jquery.min.js"></script>
<link rel="stylesheet" href="bootstrap.min.css" />
<script src="js/bootstrap.min.js"></script>
    
</head>
<body>
 <div class="container box">
  <h3 align="center">How to Import CSV Data into Mysql using Codeigniter</h3>
  <br />
<!-- Devloped by Pakainfo.com free download examples -->
  <form method="post" id="csv_import_ci" enctype="multipart/form-data">
   <div class="form-group">
    <label>Select CSV File</label>
    <input type="file" name="csv_file" id="csv_file" required accept=".csv" />
   </div>
   <br />
<!-- Devloped by Pakainfo.com free download examples -->
   <button type="submit" name="csv_import_ci" class="btn btn-info" id="csv_import_ci_btn">Import CSV</button>
  </form>
  <br />
  <div id="imported_csv_data"></div>
 </div>
</body>
</html>

<script>
$(document).ready(function(){

 load_data();

 function load_data()
 {
  $.ajax({
   url:"<?php echo base_url(); ?>csv_import/load_data",
   method:"POST",
   success:function(data)
   {
    $('#imported_csv_data').html(data);
   }
  })
 }

 $('#csv_import_ci').on('submit', function(event){
  event.preventDefault();
  
  //call ajax
  $.ajax({
   url:"<?php echo base_url(); ?>csv_import/import",
   method:"POST",
   data:new FormData(this),
   contentType:false,
   cache:false,
   processData:false,
   beforeSend:function(){
    $('#csv_import_ci_btn').html('Data Importing...');
   },
   success:function(data)
   {
    $('#csv_import_ci')[0].reset();
    $('#csv_import_ci_btn').attr('disabled', false);
    $('#csv_import_ci_btn').html('Import Done');
    load_data();
   }
  })
 });
 
});
</script>

Database: `liveatm`

--
-- Database: `liveatm`
--
------------------
-- Table structure for table `student_list`
--

CREATE TABLE IF NOT EXISTS `student_list` (
  `id` int(11) NOT NULL,
  `studfname` varchar(250) NOT NULL,
  `studlname` varchar(250) NOT NULL,
  `stud_mobile` varchar(30) NOT NULL,
  `email` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `student_list`
--
ALTER TABLE `student_list`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `student_list`
--
ALTER TABLE `student_list`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Read Also:  Laravel 5.7 Stripe Payment Gateway Example From Scratch

Example

I hope you have Got What is How to import CSV files in MySql using CodeIgnitor And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *