<?php 
require_once("includes/initialize.php");
global $database;global $connection;

if (isset($_POST['semester'])) {
    $matric = $_POST['matricno'];
    $dept = $_POST['department'];
    $level = $_POST['level'];
    $semester = $_POST['semester'];
    $session = $_POST['session'];
   
    switch(strtoupper($level).$semester){
        
        case 'ND I'.'First':
            $table='ndi_tbl';
            $semid=1;
            $prev='ND I';
            break;
        case 'ND I'.'Second':
            $table='ndi_tbl';
            $semid=2;
            $prev='ND I';
            break;
         case 'HND I'.'First':
            $table='hndi_tbl';
            $semid=1;
            $prev='HND I';
            break;
        case 'HND I'.'Second':
            $table='hndi_tbl';
            $semid=2;
            $prev='HND I';
            break;
        case 'ND II'.'First':
            $table='ndii_tbl';
            $prev='ND I';
            $semid=3;
            break;
        case 'ND II'.'Second':
            $table='ndii_tbl';
             $prev='ND I';
            $semid=4;
            break;
         case 'HND II'.'First':
            $table='hndii_tbl';
            $prev='HND I';
            $semid=3;
            break;
        case 'HND II'.'Second':
            $table='hndii_tbl';
            $prev='HND I';
            $semid=4;
            break;
            
        default:
            die('Invalid record record selection');
        
        
    }
    
    // echo $matric.' '.$dept . ' ' . $level . ' ' . $semester.' '.$table.' '.$semid;
    // $query = "SELECT course_code, course_title, course_unit FROM tbl_courses 
				// WHERE department = '$dept' AND   level = '$level' AND semester = '$semester'";
				
	   // $result = mysql_query($query);
				
    
    $allcourses=array();
    $result = COURSECLASS::fetch_registered_courses($table, $matric, $semid);
    if (!$result){
        echo "Invalid form/matric number details.";
    }else{
        
    echo $result->courses;
    
    $regCourses = explode(',',$result->courses);
    
   // print_r($regCourses);
    
   // if ($regCourses){
     //$result = $database->query($connection, $query);
    echo $result->courses;
 // $cinfo =  coursedetails($session, $level,$semeter);
    $output= '<thead>
			                <tr>
			                    <th>Course Code</th>  
			                    <th>Course Tittle</th>  
			                    <th>Course Unit</th>
								<th>Select</th>								
			                </tr>
			        	</thead>';

   // while ($r = mysql_fetch_array($result)) {
   
   foreach($regCourses as $course){
       //echo 'Course: '.$course;
       
       if (!empty($course)){
           
      
   $courseTitle=COURSECLASS::fetch_courses_title('tbl_courses', $dept, $level, $course);
        $output.=   '<tr>'
            . '<td>' . $course . '</td>'
            . '<td>' . $courseTitle->course_title . '</td>'
            . '<td>' . $courseTitle->course_unit . '</td>'
            . '<td><input type="checkbox" name="check-tab1"></td>'
            . '</tr>';
            
       }
   
    }
    //echo '</table>';
    
    // if (!empty($prev)){
        
        
    
    //  $output2= '<thead>
			 //               <tr>
			 //                   <th>Course Code</th>  
			 //                   <th>Course Tittle</th>  
			 //                   <th>Course Unit</th>
				// 				<th>Select</th>								
			 //               </tr>
			 //       	</thead>';
    
    // $courseTitle=COURSECLASS::fetch_all_courses_title('tbl_courses', $dept, $prev);
    
    //     foreach ($courseTitle as $cour){
    //          $output2 .=   '<tr>'
    //         . '<td>' . $cour->course_code . '</td>'
    //         . '<td>' . $cour->course_title . '</td>'
    //         . '<td>' . $cour->course_unit . '</td>'
    //         . '<td><input type="checkbox" name="check-tab1"></td>'
    //         . '</tr>';
    //     }
        
    // }
} 

echo $output;

// $allarray=array($allcourses,$allcourses2);
// print_r($allarray);
}
else {
    echo 'No POST';
} 


?>