Excel Import Using Codeigniter

 Views:-

               <div class="panel panel-primary ">
                <div class="panel-heading">Import Excel<a href="" class="" ></a></div>
                <table id="example2" class="table table-bordered table-striped" id="item-grid">
                 <form action="<?php echo site_url("product_details/import_view");?>" method="post" enctype="multipart/form-data" id="importFrm">
               <div class="col-xs-4" style="margin-top:10px;">
                <input class="form-control" type="file" name="file" />
                </div>
                <div class="col-xs-4" style="margin-top:10px; margin-bottom:10px;">
                 <input type="submit" class="btn btn-primary" name="importSubmit" value="IMPORT">
                </div>
            </form> 
                </table>
            </div>

Controller:-
                   
                     function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
    }



public function import_view()
    {
        $csvMimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
        if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
            if(is_uploaded_file($_FILES['file']['tmp_name'])){
               
                //open uploaded csv file with read only mode
                $csvFile = fopen($_FILES['file']['tmp_name'], 'r');
               
                // skip first line
                // if your csv file have no heading, just comment the next line
                fgetcsv($csvFile);
               
                //parse data from csv file line by line
                while(($line = fgetcsv($csvFile)) !== FALSE){
                    //check whether member already exists in database with same email
                        //insert member data into database
                        $this->db->insert("product_detail", array("category_name"=>$line[0], "product_name"=>$line[1],"product_code"=>$line[2],"rfid"=>$line[3],"quantity"=>$line[4],"unit"=>$line[5],"price"=>$line[6], "sales_date"=>$line[7], "expire_date"=>$line[8],"status"=>$line[9]));
                       
                }
               
                //close opened csv file
                //fclose($csvFile);
                fclose($csvFile);
                $data["status"] = 'Success';
                fclose($csvFile);
            }
            else{
                $data["status"] = 'Error';
            }
        }

        else{
            $data["status"] = 'Invalid file';
        }
        redirect('/product_details/product_details_view');
    }


Model:-


 public function insert($data = array())
    {
        $insert = $this->db->insert('product_detail', $data);
        if ($insert) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }


















Comments

Popular posts from this blog

Forgot password using codeigniter

Insert and Update or Edit Data using jQuery Dialogify with PHP Ajax

Facebook Login With Codeigniter