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

class Import_CSV extends CI_Controller {

	public function __construct()
	{
		parent::__construct();//call CodeIgniter's default Constructor
		$this->load->database();//load database libray manually
		$this->load->model('CSV_Model');//load Model
		
	}

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

	public function importdata()
	{ 
		if(isset($_POST["submit"]))
		{
			$file = $_FILES['file']['tmp_name'];
			$handle = fopen($file, "r");
			$c = 0;//
			while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
			{
				$user_id = $filesop[0];
				$type = $filesop[1];
				$name = $filesop[2];
				$email = $filesop[3];
				$place = $filesop[4];
				$mobile = $filesop[5];
				$zone = $filesop[6];
				
				$data2['cid']=$user_id;
				$data2['type']=$type;
				$data2['name']=$name;
				$data2['email']=$email;
				$data2['place']=$place;
				$data2['mobile']=$mobile;
				$data2['zone']=$zone;

				if($c<>0){					//SKIP THE FIRST ROW
					$this->CSV_Model->Add_CSV_Data($data2);
				}
				$edit_data['username']=$user_id;
				$edit_data['type']=$type;
			     $c = $c + 1;   
                
				
			}
			
			$this->session->set_flashdata('imported_data','Data Imported Succesfully');
            redirect(base_url().'index.php/Import_CSV/');
				
		}
	}
}
