<?php
$total_credit_registered = 0;
$total_credit_cleared = 0;
$total_credit_gpa = 0;
$total_total_mul = 0;
$total_credit_cgpa = 0;
$total_total_mul_cgpa = 0;

for ($peri = $admission_period; $peri <= $period; $peri++) {

    $qry = "SELECT distinct a.sub_code, a.grade, b.sub_crdit, b.sub_title 
	FROM $reg_file a, isr_curriculum_tbl b 
	where b.sub_list = a.sub_list 
	and a.grade in ('A+', 'A', 'B+', 'B', 'C+', 'C', 'D', 'P', 'I', 'F') 
	and a.sub_code = b.sub_code
	and a.sub_flag in ('Y','T') 
	and a.stu_roll = '$stu_roll' 
	and a.b1_sem = '$peri'
	order by a.sub_code";
    
    $ObjDB->sproc_name = $qry;
            $res_qry = $ObjDB->SelectQuery();

    $credit_registered[$peri] = 0;
    $credit_cleared[$peri] = 0;
    $credit_gpa[$peri] = 0;
    $total_mul[$peri] = 0;
    $total_mul_cgpa[$peri] = 0;

    while ($row = mysqli_fetch_array($res_qry)) {
        $sub_code = $row['sub_code'];
        $sub_credit = $row['sub_crdit'];
        $sub_title = $row['sub_title'];
        $grade = $row['grade'];

        if ($grade == "A+") {
            $var = 10;
        }
        if ($grade == "A") {
            $var = 9;
        }
        if ($grade == "B+") {
            $var = 8;
        }
        if ($grade == "B") {
            $var = 7;
        }
        if ($grade == "C+") {
            $var = 6;
        }
        if ($grade == "C") {
            $var = 5;
        }
        if ($grade == "D") {
            $var = 4;
        }
        if ($grade == "F") {
            $var = 0;
        }
        if ($grade == "I") {
            $var = 0;
        }
        if ($grade == "P") {
            $var = 0;
        }

        $mul_value = $sub_credit * $var;
        $credit_registered[$peri] = $credit_registered[$peri] + $sub_credit;
        if ($grade == "A+" or $grade == "A" or $grade == "B+" or $grade == "B" or $grade == "C+" or $grade == "C" or $grade == "D" or $grade == "P") {
            $credit_cleared[$peri] = $credit_cleared[$peri] + $sub_credit;
        }
        if ($grade == "A+" or $grade == "A" or $grade == "B+" or $grade == "B" or $grade == "C+" or $grade == "C" or $grade == "D" or $grade == "F") {
            $credit_gpa[$peri] = $credit_gpa[$peri] + $sub_credit;
            $total_mul[$peri] = $total_mul[$peri] + $mul_value;
        }
        if ($grade == "A+" or $grade == "A" or $grade == "B+" or $grade == "B" or $grade == "C+" or $grade == "C" or $grade == "D" or $grade == "F") {
            $credit_cgpa[$peri] = $credit_cgpa[$peri] + $sub_credit;
            $total_mul_cgpa[$peri] = $total_mul_cgpa[$peri] + $mul_value;
        }
    }
    $gpa[$peri] = round(($total_mul[$peri] / $credit_gpa[$peri]), 1);

    $total_credit_registered = $total_credit_registered + $credit_registered[$peri];
    $total_credit_cleared = $total_credit_cleared + $credit_cleared[$peri];
    $total_credit_gpa = $total_credit_gpa + $credit_gpa[$peri];
    $total_total_mul = $total_total_mul + $total_mul[$peri];

    $total_credit_cgpa = $total_credit_cgpa + $credit_cgpa[$peri];
    $total_total_mul_cgpa = $total_total_mul_cgpa + $total_mul_cgpa[$peri];
}
$cgpa = round(($total_total_mul_cgpa / $total_credit_cgpa), 1);
?>
<table border=0 cellspacing=0 cellpadding=1 align=center class=this width=340>

    <tr>
    <tr><th colspan=4 class=this3>Semester-wise performance</th></tr>
    <td class=this3>Semester
    <td class=this3>Credits Registered
    <td class=this3>Credits Completed
    <td class=this3>GPA
        </tr>
        <?php
        for($jj = $admission_period;  $jj <= $period; $jj++) { 
        $qsem = "select sem_name from iiser_sem where sem_num = '$jj'";
         $ObjDB->sproc_name = $qsem;
    $res_qsem = $ObjDB->SelectQuery();
    list($sem_name) = mysqli_fetch_row($res_qsem);
        ?>
    <tr>
<?php if ($credit_registered[$jj] > 0) { ?>
            <td class=this3 align=center><?php echo $sem_name; ?></td>
            <td class=this3 align=center><?php echo $credit_registered[$jj]; ?></td>
            <td class=this3 align=center><?php echo $credit_cleared[$jj]; ?></td>
            <td class=this3 align=center><?php echo number_format($gpa[$jj],1); ?></td>
<?php } ?>
        <?php } ?>
    <tr>
        <td class=this3><B>Cumulative</B>
        <td class=this3><B><?php echo $total_credit_registered; ?></B></td>
        <td class=this3><B><?php echo $total_credit_cleared ?></B></td>
        <td class=this3><B><?php echo number_format($cgpa,1); ?></B></td>
    </tr>
</table>