<?php
include("../../db.php");
//header("Access-Control-Allow-Origin: * ");

header('Content-Type: application/json');
$class = $_REQUEST['class'];
$section = $_REQUEST['section'];
$session = $_REQUEST['session'];

$type = $_REQUEST['type'];

// 	 $session=mysql_real_escape_string($session);

if (isset($_REQUEST['section']) && trim($_REQUEST['section']) != "") {

    $conwhere .= " and section='" . $_REQUEST['section'] . "' ";
}


$allsubject = $db->getRows("select subject from subject where class='$class' and session='$session'");

$allstudent = $db->getRows("select idno, admissionno,name,fname,fincome,class,section,mobile from student where session='$session' and class='$class' $conwhere  and  status=1  and issue_TC=0 order by name");

$jsonp = array();
$i = 0;
foreach ($allstudent as $stu) {
    $i++;


    $a = array("idno" => $stu['idno']);
    $b = array();
    foreach ($allsubject as $sub) {

        $examResultData = $db->getRow("select * from exammarks where school_id='1' and idno='" . $stu["idno"] . "' and type='$type' and subject='" . $sub["subject"] . "'");

        $enable = 1;

        if (count($examResultData) > 0) {
            $check = $db->getVal("select id from optional_subject where class='$_REQUEST[class]' and section='$_REQUEST[section]' and idno='$stu[idno]' and subject='$sub[subject]' and session='" . $session . "' ");
            if ($check > 0) {
                $enable = 0;
            }
            // array_push($b, array("subject" => $examResultData['subject'], "obtain" => $examResultData['obtain'], "enable" => $enable));
            array_push($b, array("subject" => $sub["subject"], "obtain" => "$examResultData[obtain]", "enable" => $enable));

        }


    }
    if (count($b) > 0) {
        $a["sub_marks"] = $b;
        array_push($jsonp, $a);
    }


}


$sqlsec = $db->getRows("select s.idno,s.class,e.subject,e.type,e.obtain,e.total,e.session from student as s inner join exammarks as e on s.idno=e.idno where s.class='$class' and e.session='$session' and type='$type' group by e.idno");
// 	$result_term = mysql_query($sqlsec);
//   while ($rowt = mysql_fetch_assoc($result_term)) 	
foreach ($sqlsec as $rowt) {

    $jsonn[] = array("idno" => $rowt['idno']);

}


$sqlsec = $db->getRows("select idno, admissionno,name,fname,fincome,class,section,mobile from student where session='$session' and class='$class' $conwhere and  status=1  and issue_TC=0 order by name");

// 	$result1 = mysql_query($sqlsec);
//   while ($rowc = mysql_fetch_assoc($result1)) 	
foreach ($sqlsec as $rowc) {
    $jsonc[] = array("idno" => $rowc['idno'], "name" => $rowc['name'], "admissionno" => $rowc['admissionno'], "fname" => $rowc['fname'], "fincome" => $rowc['fincome'], "class" => $rowc['class'], "section" => $rowc['section'], "mobile" => $rowc['mobile']);


}
if (empty($jsonc)) {
    $jsonc = array();
}
$json['status'] = 1;
$json['msg'] = "Profile found Successfully..!";
$json['data'] = array("student" => $jsonc, "mark_record" => $jsonp);

echo json_encode($json);
?>
