<?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> Asal Sekolah </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];
	        	 		$asalsek1       =	   $column[3];
//	        	 		$no_id1         =	   $column[4];

	                	// inserting values into the table
	                	$sql 				=		"INSERT INTO peserta (noreg, notest, namap, asalsek) VALUES ('$noreg1', '$notest1', '$namap1', '$asalsek1') ";
	                	$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 $asalsek1; ?> </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{
		}
	}	

}

?>