<?php session_start();
include_once "config.php";
//post variables
$class = "year7";
$arm = "amber";
$term = "1stterm";
$session = "20142015";
//generated variables
$subjects = "";
$results = "";
$subjects_sub = "";
$classarm = $class."".$arm;
$subject = array();
$resultArray = array();
function ordinal_suffix($number){
    $ends = array('th','st','nd','rd','th','th','th','th','th','th');
    if((($number % 100) >= 11) && (($number%100) <= 13)) return $number.'th';
    else
        return $number.$ends[$number%10];
}

$query_students = "SELECT * FROM beva1 WHERE password='$classarm'";
$query_subjects = "SELECT subjects FROM ".$classarm."subjects";

$result_students = $conn->query($query_students);
$result_subjects = $conn->query($query_subjects);
if($result_subjects){
    $subjectNumber = $result_subjects->num_rows;
    while($row = $result_subjects->fetch_array()){
        $subjects .= "<th colspan='4' class='rotat' style='white-space: pre-wrap;text-align: center;'><div><span>".$row['subjects']."<span></div></th>";
        $subjects_sub .= "<th class='rotate'><div><span>CAT1<span></div></th>
                            <th class='rotate'><div><span>CAT2<span></div></th>
                            <th class='rotate'><div><span>EXAM<span></div></th>
                            <th class='rotate'><div><span>TOTAL<span></div></th>";
        //echo $row['subjects']."\n";
        array_push($subject,$row['subjects']);
    }
}
$position_count = $result_students->num_rows;
while($row = $result_students->fetch_array()){
    $results = "";
    $regNo = $row['regno'];
    $studentName = $row['name'];
    $results .= "<tr>
                <td>$regNo</td>
                <td>$studentName</td>
                ";
    $grand_total = 0;
    foreach($subject as $name=>$n){
        //var_dump($name);
        //var_dump($n);
        //echo $n;
        $query_res = "SELECT * FROM `".$classarm."` WHERE REGNO='$regNo' AND Subjects='$n'";
        //echo $query_res." \n";
        $result_res = $conn->query($query_res);
        if($result_res) {
            $res = $result_res->fetch_array();
            $ca1 = $res['subject1'];
            $ca2 = $res['d1'];
            $exam = $res['ex'];
            $total = $res['tot'];
            $grand_total += $total;
            if(empty($ca1)){
                $results .= "<td>Absent</td>
                            <td>Absent</td>
                            <td>Absent</td>
                            <td>Absent</td>";
            }else{
                $results .= "<td>$ca1</td>
                            <td>$ca2</td>
                            <td>$exam</td>
                            <td>$total</td>";
            }

        }else{
            $results .= "<td>0.00</td>
                        <td>0.00</td>
                        <td>0.00</td>
                        <td>0.00</td>";
        }
    }
    $current_average = round($grand_total/$subjectNumber,2);
    if($current_average > 80.00){
        $grade = "DIAMOND";
    }else if($current_average>70.00 && $current_average<80.00){
        $grade = "GOLD";
    }else if($current_average>60.00 && $current_average<70.00){
        $grade = "SILVER";
    }else if($current_average<60.00){
        $grade = "BRONZE";
    }
    $results .= "<td>$grand_total</td>
                    <td>$current_average</td>
                    <td>$grade</td>";
    $r = array(
        "average"=>$current_average,
        "result"=>$results);
    array_push($resultArray,$r);
}

usort($resultArray, function(array $a,array $b) {
    return $b['average'] <=> $a['average'];
});

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>SCHOOL PORTAL </title>

    <meta name="description" content="" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">






    <script src="excellentexport.js"></script>

</head>
<head>
    <style>
        table {

            border-collapse: collapse;
            font: normal 75% arial, sans-serif;
        }
        th.rotate {
            /* Something you can count on */
            height: 140px;

            white-space: nowrap;
        }

        th.rotate > div {
            transform:
                /* Magic Numbers */
                translate(10px, 51px)
                    /* 45 is really 360 - 45 */
                rotate(270deg);
            width: 40px;

        }
        th.rotate > div > span {
            border-bottom: 1px solid #ccc;
            padding: 5px 10px;
        }

        .csstransforms & th.rotate {
                             height: 140px;

                         }

        td, th {
            border: 1px solid #dddddd;
            text-align: left;
            padding: 3px;

        }
        td.rotate {
            /* Something you can count on */
            height: 140px;


        }

        td.rotate > div {
            transform:
                /* Magic Numbers */
                translate(10px, 51px)
                    /* 45 is really 360 - 45 */
                rotate(270deg);
            width: 40px;
            height: 40px;
        }
        td.rotate > div > span {
            border-bottom: 1px solid #ccc;
            padding: 5px 10px;
        }

        .csstransforms & tr.rotate {
                             height: 140px;
                             white-space: nowrap;
                         }


        tr:nth-child(even) {
            background-color: #dddddd;
        }
    </style>
</head>
<body bgcolor="#fff" class="common-home">

<header>

    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div id="logo">
                    <img src="images/C11.jpg" alt="mee" class="img-responsive" />
                </div>
            </div>
        </div>
        <div class="row table-responsive">
            <div class="col-sm-12">
                <table class="table">
                    <thead>
                    <tr >
                        <th>Reg No.</th>
                        <th>Name</th>
                        <th colspan="<?php echo $subjectNumber *4; ?>">Subjects</th>
                        <th>Grand Total</th>
                        <th>Average</th>
                        <th>Grade</th>
                        <th>Position</th>
                    </tr>
                    <tr style="background-color: inherit;">
                        <th></th>
                        <th></th>
                        <?php echo $subjects ?>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                    </tr>
                    <tr>
                        <th></th>
                        <th></th>
                        <?php echo $subjects_sub ?>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php $i=1; foreach ($resultArray as $student){
                        echo $student['result']; ?>
                        <td><?php echo ordinal_suffix($i); ?></td>
                        </tr>
                    <?php $i++; } ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</header>
</body>
</html>


