<?php

	class ImportController {

		// getting connection in constructor
		function __construct($conn) {
			$this->conn 		 =		 $conn;
		}

		// function for reading csv file
		public function index() {
        	$fileName         =        "";
        	// if there is any file
	        if(isset($_FILES['file'])){
	        	// reading tmp_file name
	    		$fileName     =        $_FILES["file"]["tmp_name"];
	        }
			$counter          =        0;	 

			// if file size is not empty
	        if (isset($_FILES["file"]) && $_FILES["file"]["size"] > 0) {   
		        $file       =  fopen($fileName, "r");			        
		        // eliminating the first row of CSV file
			    fgetcsv($file);  ?>

			    <table class="table">
			    	<thead>
			    		<th> No. </th>
						<th> No. Registrasi </th>
			    		<th> No. Tes </th>
			    		<th> Nama Peserta </th>
						<th> Tempat Lahir </th>
						<th> Tanggal Lahir </th>
						<th> JK </th>
			    		<th> Asal Sekolah </th>
						<th> NISN </th>
						<th> HP Ayah </th>
						<th> email Ayah </th>
						<th> HP Ibu </th>
						<th> email Ibu </th>
						<th> Password </th>
						<th> no_id </th>
						<th> Catatan </th>
			    	</thead>
			        
		        <?php 
		        	while (($column = fgetcsv($file, 10000, ",")) !== FALSE) { 

			            $counter++;	   

			            // assigning csv column to a variable
	        	 		$noreg1      	=      $column[0];
	        	 		$notest1    	=      $column[1];
	        	 		$namap1         =      $column[2];
						$tmplhr1        =      $column[3];
						$tgllhr1        =      $column[4];
						$jk1            =      $column[5];
	        	 		$asalsek1       =	   $column[6];
						$nisn1          =      $column[7];
						$hp1            =      $column[8];
						$email1         =      $column[9];
						$hpibu1         =      $column[10];
						$emailibu1      =      $column[11];
						$pass1          =      $column[12];
						$SK1            =      $column[13];
						$Ket1           =      $column[14];
						$IS_ACTIVE1     =      $column[15];
//	        	 		$no_id1         =	   $column[16];

	                	// inserting values into the table
	                	$sql 				=		"INSERT INTO peserta (noreg, notest, namap, tmplhr, tgllhr, jk, asalsek, nisn, hp, email, hpibu, emailibu, pass, SK, Ket, IS_ACTIVE) VALUES ('$noreg1', '$notest1', '$namap1', '$tmplhr1', '$tgllhr1', '$jk1', '$asalsek1', '$nisn1', '$hp1', '$email1', '$hpibu1', '$emailibu1', '$pass1', '$SK1', '$Ket1', '$IS_ACTIVE1') ";
	                	$result 			=		$this->conn->query($sql);

	                	if($result == 1): ?>                		
	                    		<tr>
									<td> <?php echo $counter; ?> </td>
									<td> <?php echo $noreg1; ?> </td>
									<td> <?php echo $notest1; ?> </td>
									<td> <?php echo $namap1; ?> </td>
									<td> <?php echo $tmplhr1; ?> </td>
									<td> <?php echo $tgllhr1; ?> </td>
									<td> <?php echo $jk1; ?> </td>
									<td> <?php echo $asalsek1; ?> </td>
									<td> <?php echo $nisn1; ?> </td>
									<td> <?php echo $hp1; ?> </td>
									<td> <?php echo $email1; ?> </td>
									<td> <?php echo $hpibu1; ?> </td>
									<td> <?php echo $emailibu1; ?> </td>
									<td> <?php echo $pass1; ?> </td>
									<td> <?php echo $no_id1; ?> </td>
	     							<td> <?php echo "<label class='text-success'>Success </label> " .date('d-m-Y H:i:s');?> </td>
								</tr>
	                	<?php endif;
				}
				 ?>
				</table>

				<?php
			}

		else{
		}
	}	

}

?>