<?php include("session_controller.php"); ?>
<?php
mysqli_select_db($conn, $database_conn);
$query_students = NULL;
if(isset($_GET['search']) && ($_GET['search'] != "")){
$query_students = "SELECT s_no, admission_no, surname, name, middle, whereabout, class, no_of_exits, location, address FROM students WHERE whereabout LIKE '%".$_GET['search']. "%'";	
}
else{
$query_students = "SELECT s_no, admission_no, surname, name, middle, whereabout,class, no_of_exits, location, address FROM students";
}
$students = mysqli_query($conn, $query_students) or die(mysqli_error($conn));
$row_students = mysqli_fetch_assoc($students);
$totalRows_students = mysqli_num_rows($students);

?>
<?php if($totalRows_students > 0){ ?>
<div class="table-responsive">
<table width="100%" class="table table-striped table-bordered"> 
<thead>
    <tr>
      <th width="44">S/N </th><th width="255">Full Name </th><th width="100">Whereabout</th><th width="97">Class </th><th width="140">Admission Nos </th>
       <?php if(isset($_SESSION['MM_UserGroup']) && ($_SESSION['MM_UserGroup'] == "admin")){?>
       <th>  Additional Info </th>
      <th width="107">Action 1</th><th width="107">Action 2</th>
      <?php } ?>
    </tr>
    </thead>
    <tbody>
<?php 
$counter = 0;
$d="active";
do { ?>
    <tr>
      <td><?php echo ++$counter; ?></td><td><a href="record.php?id2=<?php echo $row_students['admission_no']; ?>" title="View Medical record"><?php echo $row_students['surname']; ?><?php echo " "; ?> <?php echo $row_students['name']; ?><?php echo " "; ?><?php echo $row_students['middle']; ?> </a></td>
<td>
<?php 
/*
$location_query = "SELECT location FROM exit_records WHERE student_adm_no = '".$row_students['admission_no']."' AND end_date > NOW() ";	
$location = mysqli_query($location_query, $conn) or die(mysqli_error());
$row_location = mysqli_fetch_assoc($location);
if($row_location){
	echo $row_location['location'];
}
else{
	echo "School";
}
*/
echo $row_students['whereabout'];
?>
</td>
 <td><?php echo $row_students['class']; ?> </td><td> <?php echo $row_students['admission_no']; ?></td>
 <?php if(isset($_SESSION['MM_UserGroup']) && ($_SESSION['MM_UserGroup'] == "admin")){?>
 <td><?php echo $row_students['address']; ?> </td>
	 <td width="107"><form action="add_history.php" method="get"><input name="s_no" value="<?php echo $row_students['s_no']; ?>" type="hidden" /><input name="admission_no" value="<?php echo $row_students['admission_no']; ?>" type="hidden" /><button type="submit" name="up" class="btn btn-primary">Update</button></form> </td>
     <td width="107"><form action="delete_student.php" method="get"><input name="admission_no" value="<?php echo $row_students['admission_no']; ?>" type="hidden" /><button type="submit" name="up" class="btn btn-primary" onClick="return check_confirmation();">Delete</button></form> </td>
     <?php } ?>
    </tr>
    <?php 
	if($d == "active"){
    $d = "success";
    }
    else{
    $d ="active";
    }
	?>
  <?php } while ($row_students = mysqli_fetch_assoc($students)); ?>
  <!-- close query div -->
  </tbody>
 </table>
 </div>
<?php } 
else{
?>
<h3> No Matches Found !!! </h3>
<?php } ?>
