<style>.top-bar-minimize{display:none;}
thead, tfoot {display: table-header-group;}
.top-bar+.well,.accordion .accordion-heading+.accordion-body{display: inline-block !important;width: 100% !important;overflow: inherit !important;} 
</style>
<?php
if($_POST){
	$mysqlImportFilename ='';
	if(isset($_FILES["photo"]["name"]) && !empty($_FILES["photo"]["name"])){
		$lfilename = basename($_FILES['photo']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		if(in_array($lext,array('csv')))
		{
			$lnewfile=md5(microtime()).".".$lext;
			if(move_uploaded_file($_FILES['photo']['tmp_name'],"media/".$lnewfile))
			{
				// Read in entire file
				$file = "media/".$lnewfile;$ij=0;
				// Loop through each line
				if (($handle = fopen($file, "r")) !== FALSE) {
					while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
						$ij++;
						if($ij!=1){
							//echo "<pre>";print_r($data);echo "</pre>";
							$query=$db->query("INSERT INTO `student`(`admissionclass`, `class`, `section`, `bus`, `doa`, `sibling`, `medium`, `stype`, `rollno`, `admissionno`, `name`, `dob`, `fname`, `mname`, `cast`, `nstate`, `address`, `mobile`, `email`, `foccupation`, `photo`, `udate`, `session`, `status`, `issue_TC`, `gender`, `feducation`, `meducation`, `old_school`, `subject`, `sssmid`, `mincome`, `fincome`, `TC`, `school_id`, `added_by`, `height`, `weight`, `hygiene`) VALUES('".implode("','",str_replace("'","\'",str_replace('"','\"',$data)))."')");
							
							//echo $query."<hr/>";
							$instalmentAry=array("idno"=>$db->getLastId(),
												 "class"=>$data[1],
												 'session'=>$data[22],
												 "status"=>1,
												 "school_id"=>$data[34],
												 "added_by"=>$data[35],
							);
							$i=0;$j=0;
							$installmentClass=$db->getRow("select installment,fee from class where class='".$data[1]."'");
							$installmentbus=$db->getRow("select installment,fee from bus where id='".$data[3]."'");
							$classins=($installmentClass["fee"]/$installmentClass["installment"]);
							$busins=($installmentbus["fee"]/$installmentbus["installment"]);
							for($i=1;$i<=$installmentClass["installment"];$i++){
								$list="i".$i;
								$instalmentAry[$list]=$classins;	
							}
							for($i=1;$i<=$installmentbus["installment"];$i++){
								$list="b".$i;
								$instalmentAry[$list]=$busins;	
							}
							$instalmentAry["total"]=$installmentClass["fee"];
							$instalmentAry["bus"]=$installmentbus["fee"];
							$db->insertAry("installment",$instalmentAry);
						}
					}
					fclose($handle);
				}else{
					echo "File error ".$handle ;
				}
				
				
				
				
				$_SESSION["msg"]="Student imported successfully";
				//////////////////////////**************setting backup table***********////////////////////////////
				if($db->getErMsg()=="")
				redirect("?page_id=import_student");
				else
				$alert_err[] = $db->getErMsg();
				
			}
		}else{
			$alert_err[]="Invalid file extension";
		}
	}
	
}
if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
?> 
<div class="row-fluid">
        <form method="post" action="" enctype="multipart/form-data">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
                	
					<h3><i class="icon-cog"></i> Import File</h3>
                    
				</div>
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
				<div class="well no-padding">

						<input type="hidden" name="ss" value="1">
                        <div class="control-group">
							<label class="control-label" for="msg">CSV File</label>
							<div class="controls">
                            <a href="<?php echo URL_ROOT ?>media/student.csv" style="float:right" >(Download Sample)</a>
								<input type="file" id="file" name="photo" class="input-block-level">
							</div>
						</div>
						<div class="form-actions">
							<button type="submit" class="btn btn-primary"> import</button>
							<button type="reset" class="btn"> Cancel</button>
						</div>
                        
				</div>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box -->

        </form>
	</div>