<?php
error_reporting(0);
ob_start();
session_start();
require_once("../common/connect.php");
$ses = $_SESSION['ses_id'];

$contents="S.No, SID, Student Name, Course, Sem, Fathers Name, Mothers Name, Address, City, State, Email, Contact No, DOB, Parent Contact No, 10th, 12th, Diploma, Graduation, Post Graduation\n";

//Mysql query to get records from datanbase
//You can customize the query to filter from particular date and month etc...Which will depends your database structure.
$user_query = mysql_query("SELECT * FROM  `student_registration` WHERE  `step` >8 AND  `sesion` =9");
$count=0;

//While loop to fetch the records
while($row = mysql_fetch_assoc($user_query))
{
$GetPersonal=mysql_query("SELECT * FROM `step1_perosonal` WHERE `sid` = '$row[sid]'");
$GetPersonal1=mysql_fetch_assoc($GetPersonal);

$GetContact=mysql_query("SELECT * FROM  `step1_contact` WHERE  `sid` ='$row[sid]'");
$GetContact1=mysql_fetch_assoc($GetContact);

$GetCourse=mysql_query("SELECT * FROM `sgvu_courses` WHERE `course_id` ='$GetPersonal1[program]'");
$GetCourse1=mysql_fetch_assoc($GetCourse);

$GetSem=mysql_query("SELECT * FROM  `sgvu_stu_sem_allocate` WHERE  `sid` ='$row[sid]'");
$GetSem1=mysql_fetch_assoc($GetSem);

$add1=$GetContact1['add1'];
$addA1=str_replace(","," ", $add1);
$add2=$GetContact1['add2'];
$addA2=str_replace(","," ", $add2);

$GetState=mysql_query("SELECT * FROM  `state` WHERE  `ids` ='$GetContact1[state]'");
$GetState1=mysql_fetch_assoc($GetState);

$GetCity=mysql_query("SELECT * FROM  `city` WHERE  `city_id` ='$GetContact1[city]'");
$GetCity1=mysql_fetch_assoc($GetCity);

$GetTeanth=mysql_query("SELECT * FROM  `step2_education` WHERE  `sid` ='$row[sid]' AND  `education_level` =1");
$GetTeanth1=mysql_fetch_assoc($GetTeanth);

		if($GetTeanth1['max_marks']!='0' || $GetTeanth1['max_marks']!='')
		{
		$TenPer=$GetTeanth1['obtained_marks']*100/$GetTeanth1['max_marks'];
		}
		else
		{
		$TenPer='NA';
		}
$TenPer1=round($TenPer, 2);

$GetTwelth=mysql_query("SELECT * FROM  `step2_education` WHERE  `sid` ='$row[sid]' AND  `education_level` =2");
$GetTwelth1=mysql_fetch_assoc($GetTwelth);
		if($GetTeanth1['max_marks']>'0' || $GetTeanth1['max_marks']!='')
		{
		$Twelth=$GetTwelth1['obtained_marks']*100/$GetTwelth1['max_marks'];
		}
		else
		{
		$Twelth='NA';
		}
			$Twelth1=round($Twelth, 2);
		


$count+=1;
$t=$row['time'];


$contents.=$count.",";
$contents.=$row['sid'].",";
$contents.=strtoupper($GetPersonal1['firstname']).' '.strtoupper($GetPersonal1['middlename']).' '.strtoupper($GetPersonal1['lastname']).",";
$contents.=strtoupper($GetCourse1['coursename']).",";
$contents.=strtoupper($GetSem1['sem']).",";
$contents.=strtoupper($GetPersonal1['father_name']).",";
$contents.=strtoupper($GetPersonal1['mother_name']).",";
$contents.=$addA1.' '.$addA2.",";
$contents.=strtoupper($GetCity1['cityname']).",";
$contents.=strtoupper($GetState1['state']).",";
$contents.=$row['email'].",";
$contents.=$row['mobile'].",";
$contents.=$GetPersonal1['dob'].",";
$contents.=$GetContact1['parent_mobile'].",";
$contents.=$TenPer1.",";
$contents.=$Twelth1."\n";
}

$contents = strip_tags($contents); 

header("Content-Disposition: attachment; filename=total-admission-list.csv");
print $contents;

?>