<?php
include("./db.php");
$ch=$_REQUEST["type"];$status="";
if($ch=="detail"){
		$table=" student as s";
		$result=$db->getRows("SELECT s.* FROM ".$table." where s.school_id='".$_SESSION["admin"]["school_id"]."' and s.issue_TC=0 and ".$_REQUEST['result']." order by s.name");
		//echo $db->getLastQuery();
		//print_r($result);
		if(is_array($result) && count($result)>0){
			define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
					/** Include PHPExcel */
			require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
			$objPHPExcel = new PHPExcel();
			$objPHPExcel->setActiveSheetIndex(0)
						->setCellValue('A1', 'Adm. No')
						->setCellValue('B1', 'Name')
						->setCellValue('C1', 'Father')
						->setCellValue('D1', 'Mother')
						->setCellValue('E1', 'Class')
						->setCellValue('F1', 'Section')
						->setCellValue('G1', 'Admission Date')
						->setCellValue('H1', 'DOB')
						->setCellValue('I1', 'SEX')
						->setCellValue('J1', 'Address')
						->setCellValue('K1', 'Mobile')
						->setCellValue('L1', 'House');
			$i=1;
			foreach($result as $student){$i++;
				$house=$db->getVal("Select name from house where id='".$student['sssmid']."'");
				$objPHPExcel->setActiveSheetIndex(0)
							->setCellValue('A'.$i.'', ''.$student["admissionno"].'')
							->setCellValue('B'.$i.'', ''.$student["name"].'')
							->setCellValue('C'.$i.'', ''.$student["fname"].'')
							->setCellValue('D'.$i.'', ''.$student["mname"].'')
							->setCellValue('E'.$i.'', ''.$student["class"].'')
							->setCellValue('F'.$i.'', ''.$student["section"].'')
							->setCellValue('G'.$i.'', ''.$student["doa"].'')
							->setCellValue('H'.$i.'', ''.$student["dob"].'')
							->setCellValue('I'.$i.'', ''.$student["gender"].'')
							->setCellValue('J'.$i.'', ''.$student["address"].'')
							->setCellValue('K'.$i.'', ''.$student["mobile"].'')
							->setCellValue('L'.$i.'', ''.$house.'');
			}
			//$objPHPExcel->getActiveSheet()->setTitle('marks123');
			$objPHPExcel->setActiveSheetIndex(0);
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
			//echo $root=getcwd()."\\files\detail_".$student["class"]."_".$student["section"].".xls";
			$file="D:\\erp_excel";
			if(!is_dir($file)) {
				 mkdir('D:\\erp_excel', 0777, true);
			}
			$root="D:\\erp_excel\personal_detail_".$student["class"]."_".$student["section"].".xls";
			$objWriter->save($root);
			echo 'Excel File have been created in ' , $root , EOL;
		}
}
elseif($ch=="bus_detail"){
		$table=" student as s";
		$result=$db->getRows("SELECT s.* FROM ".$table." where s.school_id='".$_SESSION["admin"]["school_id"]."' and s.issue_TC=0 and ".$_REQUEST['result']." order by s.name");
		//echo $db->getLastQuery();
		//print_r($result);
		if(is_array($result) && count($result)>0){
			define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
					/** Include PHPExcel */
			require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
			$objPHPExcel = new PHPExcel();
			$objPHPExcel->setActiveSheetIndex(0)
						->setCellValue('A1', 'Adm. No')
						->setCellValue('B1', 'Name')
						->setCellValue('C1', 'Father')
						->setCellValue('D1', 'Mother')
						->setCellValue('E1', 'Class')
						->setCellValue('F1', 'Section')
						->setCellValue('G1', 'Admission Date')
						->setCellValue('H1', 'DOB')
						->setCellValue('I1', 'SEX')
						->setCellValue('J1', 'Address')
						->setCellValue('K1', 'Mobile')
						->setCellValue('L1', 'House');
			$i=1;
			foreach($result as $student){$i++;
				$house=$db->getVal("Select name from house where id='".$student['sssmid']."'");
				$objPHPExcel->setActiveSheetIndex(0)
							->setCellValue('A'.$i.'', ''.$student["admissionno"].'')
							->setCellValue('B'.$i.'', ''.$student["name"].'')
							->setCellValue('C'.$i.'', ''.$student["fname"].'')
							->setCellValue('D'.$i.'', ''.$student["mname"].'')
							->setCellValue('E'.$i.'', ''.$student["class"].'')
							->setCellValue('F'.$i.'', ''.$student["section"].'')
							->setCellValue('G'.$i.'', ''.$student["doa"].'')
							->setCellValue('H'.$i.'', ''.$student["dob"].'')
							->setCellValue('I'.$i.'', ''.$student["gender"].'')
							->setCellValue('J'.$i.'', ''.$student["address"].'')
							->setCellValue('K'.$i.'', ''.$student["mobile"].'')
							->setCellValue('L'.$i.'', ''.$house.'');
			}
			//$objPHPExcel->getActiveSheet()->setTitle('marks123');
			$objPHPExcel->setActiveSheetIndex(0);
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
			//echo $root=getcwd()."\\files\detail_".$student["class"]."_".$student["section"].".xls";
			$file="D:\\erp_excel";
			if(!is_dir($file)) {
				 mkdir('D:\\erp_excel', 0777, true);
			}
			$root="D:\\erp_excel\bus_detail_".$student["class"]."_".$student["section"].".xls";
			$objWriter->save($root);
			echo 'Excel File have been created in ' , $root , EOL;
			
			
		}
}
