<?php
session_start();


include_once ('../../../db_connect/db.php');
//include_once('../functions.php');
if (isset($_SESSION['student_id'])) {
    $student_id = $_SESSION['student_id'];
    $type_id = $_SESSION['type_id'];
    $session =6;
    //get datas from the form
    $iddd = $_POST['site'];
    $output = '';
    $bedspace = '';
    $category_id = "";
    $site_id = "";

    if($type_id==1)
    {
      //get user's details
      $q = "SELECT id, CONCAT_WS(' ', first_name, other_names) as name, `phone_no`, `gender`,`number`,
      `email`,`level`,`programme_id`, `image` FROM students_prence WHERE id='$student_id'";
      $r = mysqli_query($conn_1, $q);
      //in the table below result_table stands for resullt fetch throug seraching table(ajax)

      if (mysqli_num_rows($r) > 0) { //show table

          while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
          //get programme Title

          {
              $gender = $row['gender'];
              if($gender=='M'){
                $gender = 'Male';
              }else{
                $gender = 'Female';
              }
              //QuickHashIntHash
              $img = $row['image'];

              if ($img == "") {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              } else {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              }
              //get hostel block

              //get hostel block
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_blocks WHERE campus_id = '$iddd' and gender = '$gender' AND (id !='3' OR id !='4' OR id !='5' OR id !='6') ORDER BY RAND() ") or die(mysqli_error($conn_1));

              while ($ok = mysqli_fetch_assoc($result)) {

                  $title = $ok['title'];
                  $site_id = $ok['campus_id'];
                  $cat_id = $ok['id'];
                  $total_room = $ok['bed_space'];
                  if ($title == "") {
                      $title = "no available hostel block yet";
                  } else {
                      $title == $title;
                  }

                  // checking if available free block SPACE
                  $count_Cat = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE block_id='$cat_id' AND status='1' ");
                  if ($count_Cat) {
                      $sql_row_cat = mysqli_num_rows($count_Cat);

                      if ($sql_row_cat < $total_room) {
                          $category_id = $cat_id;
                          break;
                      } else {
                          $category_id = "";

                      }
                  }
              }

  			//get hostel room
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_rooms WHERE block_id ='$category_id' AND campus_id='$site_id' AND reserved='0' ORDER BY RAND() ") or die(mysqli_error($conn_1));
              if ($result) {
  				$num_rooms = mysqli_num_rows($result);

                  if ($num_rooms > 0) {

                      while ($okk = mysqli_fetch_assoc($result)) {
                          $room_id = $okk['id'];
                          $room_title = $okk['title'];
                          $total_bedspace = $okk['bed_space'];

                          $count = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE room_id='$room_id' AND status='1' ");
                          if ($count) {
                              $sql_row_room = mysqli_num_rows($count);
                              if ($sql_row_room < $total_bedspace) {
                                  $room_id == $room_id;

                                  $chk = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE student_id='$student_id' AND status='1' AND type_id='$type_id' ") or die(mysqli_error($conn_1));
                                  if ($chk) {

                                      $sql_row = mysqli_num_rows($chk);
                                      if ($sql_row > 0) {
                                          $vv = mysqli_fetch_assoc($chk);
                                          $cat_idd = $vv['block_id'];
                                          $room_idd = $vv['room_id'];

                                          //get his room title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_idd' ") or die(mysqli_error($conn_1));
                                          $sql_room = mysqli_fetch_assoc($allocate);
                                          $Room_title = $sql_room['title'];

                                          //get his campus title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                          $sql_site = mysqli_fetch_assoc($allocate);
                                          $Site_title = $sql_site['title'];

                                          //get his block title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_idd' ") or die(mysqli_error($conn_1));
                                          $sql_category = mysqli_fetch_assoc($allocate);
                                          $Cat_title = $sql_category['title'];

                                          echo '
              										<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
              											<div class="list-group">
              												<ul class="list-group">
              													<li class="list-group-item list-group-item-success">
              														<h3>My Bed Space Information/ Make Payment</h3>
              													</li>
              												</ul>
              											</div>
              										</div>

              										<div class="panel panel-default">
              											<div class="panel-heading">
              												<h3 class="panel-title">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
              											</div>
              											<div class="panel-body">';

                                                      echo '
              										<label class="alert alert-danger">
              											You have already been allocated room, proceed to payment please!
              										</label><br>
              										<table width="100%" class="table">
              											<tr>
              												<td width="10%">' . $pic . '</td>
              												<td width="90%">
              												<table width="100%">
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/></td>
              													</tr>
              												</table>
              												<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payments" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
              											</td>
              										</tr>

              									</table><hr/>';

                                      } else {
                                          //insert into hostel allocation table

                                          $insert = mysqli_query($conn_1, "INSERT INTO hostel_allocation(school_id,type_id,campus_id,block_id,room_id,student_id,`status`,chk_in_date_time, `session_id`,`added_by`)
  												                VALUES('1','$type_id','$iddd','$cat_id','$room_id','$student_id','1',NOW(), '$session','$student_id')") or die(mysql_error($conn_1));
                                          if ($insert) {
                                              mysqli_query($conn_1, "UPDATE students_nce SET hostel_allocation = '1' WHERE id='$student_id' ");

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_id' ") or die(mysqli_error($conn_1));
                                              $sql_room = mysqli_fetch_assoc($allocate);
                                              $Room_title = $sql_room['title'];

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                              $sql_site = mysqli_fetch_assoc($allocate);
                                              $Site_title = $sql_site['title'];

                                              //get his category title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_id' ") or die(mysqli_error($conn_1));
                                              $sql_category = mysqli_fetch_assoc($allocate);
                                              $Cat_title = $sql_category['title'];

                                              echo '

          												<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
          															<div class="list-group">
          																<ul class="list-group">
          																	<li class="list-group-item list-group-item-success">
          																		<h3>My Bed Space Information/ Make Payment</h3>
          																	</li>
          																</ul>



          															</div>


          												</div>

          												<div class="panel panel-default">
          													<div class="panel-heading">
          														<h3 class="panel-title" style="font-size:20px; font-weight:bold;">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
          													</div>
          													<div class="panel-body">';

                                                      echo '
          														<br>
          														<table width="100%" class="table">
          															<tr>
          																<td width="10%">' . $pic . '</td>
          																<td width="90%">
          																	<table width="100%">
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/>


          																			</td>
          																		</tr>
          																	</table>
          																	<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payment" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
          																</td>
          															</tr>

          														</table><hr/>';

                                          }

                                      }
                                  }

                                  break;
                              } else {

                                  $room_title = "!No accommodation at the moment, Check back much later";

                                  $output = '<br><br>



            										<div class="alert alert-danger">
            										  ' . strtoupper($room_title) . '
            										</div>';

                              }

                          }

                      }

                  } else {
                      $room_title = "no available room yet, check back later!";

                      $output = '<br><br>



  								<div class="alert alert-danger">
  								  ' . strtoupper($room_title) . '
  								</div>


  		        ';
                  }

              }
          }

      }
    }else if($type_id== 2)
    {

      //get user's details
       $q = "SELECT id, CONCAT_WS(' ', first_name, other_names) as name, `phone_no`, `gender`,`number`,
      `email`,`level`,`programme_id`, `image` FROM students_nce WHERE id='$student_id'";
      $r = mysqli_query($conn_1, $q);
      //in the table below result_table stands for resullt fetch throug seraching table(ajax)

      if (mysqli_num_rows($r) > 0) { //show table

          while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
          //get programme Title

          {
              $gender = $row['gender'];
              if($gender=='M'){
                $gender = 'Male';
              }else{
                $gender = 'Female';
              }

              //QuickHashIntHash
              $img = $row['image'];

              if ($img == "") {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              } else {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              }
              //get hostel block

              //get hostel block
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_blocks WHERE campus_id = '$iddd' and gender = '$gender' AND (id !='3' OR id !='4' OR id !='5' OR id !='6') ORDER BY RAND() ") or die(mysqli_error($conn_1));

              while ($ok = mysqli_fetch_assoc($result)) {

                  $title = $ok['title'];
                  $site_id = $ok['campus_id'];
                  $cat_id = $ok['id'];
                  $total_room = $ok['bed_space'];
                  if ($title == "") {
                      $title = "no available hostel block yet";
                  } else {
                      $title == $title;
                  }

                  // checking if available free block SPACE
                  $count_Cat = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE block_id='$cat_id' AND status='1' ");
                  if ($count_Cat) {
                      $sql_row_cat = mysqli_num_rows($count_Cat);

                      if ($sql_row_cat < $total_room) {
                          $category_id = $cat_id;
                          break;
                      } else {
                          $category_id = "";

                      }
                  }
              }

  			//get hostel room
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_rooms WHERE block_id ='$category_id' AND campus_id='$site_id' AND reserved='0' ORDER BY RAND() ") or die(mysqli_error($conn_1));
              if ($result) {
  				$num_rooms = mysqli_num_rows($result);

                  if ($num_rooms > 0) {

                      while ($okk = mysqli_fetch_assoc($result)) {
                          $room_id = $okk['id'];
                          $room_title = $okk['title'];
                          $total_bedspace = $okk['bed_space'];

                          $count = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE room_id='$room_id' AND status='1' ");
                          if ($count) {
                              $sql_row_room = mysqli_num_rows($count);
                              if ($sql_row_room < $total_bedspace) {
                                  $room_id == $room_id;

                                  $chk = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE student_id='$student_id' AND status='1' AND type_id='$type_id' ") or die(mysqli_error($conn_1));
                                  if ($chk) {

                                      $sql_row = mysqli_num_rows($chk);
                                      if ($sql_row > 0) {
                                          $vv = mysqli_fetch_assoc($chk);
                                          $cat_idd = $vv['block_id'];
                                          $room_idd = $vv['room_id'];

                                          //get his room title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_idd' ") or die(mysqli_error($conn_1));
                                          $sql_room = mysqli_fetch_assoc($allocate);
                                          $Room_title = $sql_room['title'];

                                          //get his room title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                          $sql_site = mysqli_fetch_assoc($allocate);
                                          $Site_title = $sql_site['title'];

                                          //get his category title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_idd' ") or die(mysqli_error($conn_1));
                                          $sql_category = mysqli_fetch_assoc($allocate);
                                          $Cat_title = $sql_category['title'];

                                          echo '
              										<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
              											<div class="list-group">
              												<ul class="list-group">
              													<li class="list-group-item list-group-item-success">
              														<h3>My Bed Space Information/ Make Payment</h3>
              													</li>
              												</ul>
              											</div>
              										</div>

              										<div class="panel panel-default">
              											<div class="panel-heading">
              												<h3 class="panel-title">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
              											</div>
              											<div class="panel-body">';

                                                      echo '
              										<label class="alert alert-danger">
              											You have already been allocated room, proceed to payment please!
              										</label><br>
              										<table width="100%" class="table">
              											<tr>
              												<td width="10%">' . $pic . '</td>
              												<td width="90%">
              												<table width="100%">
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/></td>
              													</tr>
              												</table>
              												<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payments" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
              											</td>
              										</tr>

              									</table><hr/>';

                                      } else {
                                          //insert into hostel allocation table

                                          $insert = mysqli_query($conn_1, "INSERT INTO hostel_allocation(school_id,type_id,campus_id,block_id,room_id,student_id,`status`,chk_in_date_time, `session_id`,`added_by`)
  												                VALUES('1','$type_id','$iddd','$cat_id','$room_id','$student_id','1',NOW(), '$session','$student_id')") or die(mysql_error($conn_1));
                                          if ($insert) {
                                              mysqli_query($conn_1, "UPDATE students_nce SET hostel_allocation = '1' WHERE id='$student_id' ");

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_id' ") or die(mysqli_error($conn_1));
                                              $sql_room = mysqli_fetch_assoc($allocate);
                                              $Room_title = $sql_room['title'];

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                              $sql_site = mysqli_fetch_assoc($allocate);
                                              $Site_title = $sql_site['title'];

                                              //get his category title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_id' ") or die(mysqli_error($conn_1));
                                              $sql_category = mysqli_fetch_assoc($allocate);
                                              $Cat_title = $sql_category['title'];

                                              echo '

          												<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
          															<div class="list-group">
          																<ul class="list-group">
          																	<li class="list-group-item list-group-item-success">
          																		<h3>My Bed Space Information/ Make Payment</h3>
          																	</li>
          																</ul>



          															</div>


          												</div>

          												<div class="panel panel-default">
          													<div class="panel-heading">
          														<h3 class="panel-title" style="font-size:20px; font-weight:bold;">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
          													</div>
          													<div class="panel-body">';

                                                      echo '
          														<br>
          														<table width="100%" class="table">
          															<tr>
          																<td width="10%">' . $pic . '</td>
          																<td width="90%">
          																	<table width="100%">
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/>


          																			</td>
          																		</tr>
          																	</table>
          																	<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payment" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
          																</td>
          															</tr>

          														</table><hr/>';

                                          }

                                      }
                                  }

                                  break;
                              } else {

                                  $room_title = "!No accommodation at the moment, Check back much later";

                                  $output = '<br><br>



            										<div class="alert alert-danger">
            										  ' . strtoupper($room_title) . '
            										</div>';

                              }

                          }

                      }

                  } else {
                      $room_title = "no available room yet, check back later!";

                      $output = '<br><br>



  								<div class="alert alert-danger">
  								  ' . strtoupper($room_title) . '
  								</div>


  		        ';
                  }

              }
          }

      }
    }else if($type_id== 3)
    {
      //get user's details
      $q = "SELECT id, CONCAT_WS(' ', first_name, other_names) as name, `phone_no`, `gender`,`number`,
      `email`,`level`,`programme_id`, `image` FROM students_nce WHERE id='$student_id'";
      $r = mysqli_query($conn_2, $q);
      //in the table below result_table stands for resullt fetch throug seraching table(ajax)

      if (mysqli_num_rows($r) > 0) { //show table

          while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
          //get programme Title

          {
              $gender = $row['gender'];
              if($gender=='M'){
                $gender = 'Male';
              }else{
                $gender = 'Female';
              }

              //QuickHashIntHash
              $img = $row['image'];

              if ($img == "") {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              } else {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              }
              //get hostel block

              //get hostel block
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_blocks WHERE campus_id = '$iddd' and gender = '$gender' AND (id !='3' OR id !='4' OR id !='5' OR id !='6') ORDER BY RAND() ") or die(mysqli_error($conn_1));

              while ($ok = mysqli_fetch_assoc($result)) {

                  $title = $ok['title'];
                  $site_id = $ok['campus_id'];
                  $cat_id = $ok['id'];
                  $total_room = $ok['bed_space'];
                  if ($title == "") {
                      $title = "no available hostel block yet";
                  } else {
                      $title == $title;
                  }

                  // checking if available free block SPACE
                  $count_Cat = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE block_id='$cat_id' AND status='1' ");
                  if ($count_Cat) {
                      $sql_row_cat = mysqli_num_rows($count_Cat);

                      if ($sql_row_cat < $total_room) {
                          $category_id = $cat_id;
                          break;
                      } else {
                          $category_id = "";

                      }
                  }
              }

  			//get hostel room
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_rooms WHERE block_id ='$category_id' AND campus_id='$site_id' AND reserved='0' ORDER BY RAND() ") or die(mysqli_error($conn_1));
              if ($result) {
  				$num_rooms = mysqli_num_rows($result);

                  if ($num_rooms > 0) {

                      while ($okk = mysqli_fetch_assoc($result)) {
                          $room_id = $okk['id'];
                          $room_title = $okk['title'];
                          $total_bedspace = $okk['bed_space'];

                          $count = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE room_id='$room_id' AND status='1' ");
                          if ($count) {
                              $sql_row_room = mysqli_num_rows($count);
                              if ($sql_row_room < $total_bedspace) {
                                  $room_id == $room_id;

                                  $chk = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE student_id='$student_id' AND status='1' AND type_id='$type_id' ") or die(mysqli_error($conn_1));
                                  if ($chk) {

                                      $sql_row = mysqli_num_rows($chk);
                                      if ($sql_row > 0) {
                                          $vv = mysqli_fetch_assoc($chk);
                                          $cat_idd = $vv['block_id'];
                                          $room_idd = $vv['room_id'];

                                          //get his room title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_idd' ") or die(mysqli_error($conn_1));
                                          $sql_room = mysqli_fetch_assoc($allocate);
                                          $Room_title = $sql_room['title'];

                                          //get his room title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                          $sql_site = mysqli_fetch_assoc($allocate);
                                          $Site_title = $sql_site['title'];

                                          //get his category title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_idd' ") or die(mysqli_error($conn_1));
                                          $sql_category = mysqli_fetch_assoc($allocate);
                                          $Cat_title = $sql_category['title'];

                                          echo '
              										<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
              											<div class="list-group">
              												<ul class="list-group">
              													<li class="list-group-item list-group-item-success">
              														<h3>My Bed Space Information/ Make Payment</h3>
              													</li>
              												</ul>
              											</div>
              										</div>

              										<div class="panel panel-default">
              											<div class="panel-heading">
              												<h3 class="panel-title">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
              											</div>
              											<div class="panel-body">';

                                                      echo '
              										<label class="alert alert-danger">
              											You have already been allocated room, proceed to payment please!
              										</label><br>
              										<table width="100%" class="table">
              											<tr>
              												<td width="10%">' . $pic . '</td>
              												<td width="90%">
              												<table width="100%">
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/></td>
              													</tr>
              												</table>
              												<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payments" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
              											</td>
              										</tr>

              									</table><hr/>';

                                      } else {
                                          //insert into hostel allocation table

                                          $insert = mysqli_query($conn_1, "INSERT INTO hostel_allocation(school_id,type_id,campus_id,block_id,room_id,student_id,`status`,chk_in_date_time, `session_id`,`added_by`)
  										VALUES('1','$type_id','$iddd','$cat_id','$room_id','$student_id','1',NOW(), '$session','$student_id')") or die(mysql_error($conn_1));
                                          if ($insert) {
                                              mysqli_query($conn_2, "UPDATE students_nce SET hostel_allocation = '1' WHERE id='$student_id' ");

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_id' ") or die(mysqli_error($conn_1));
                                              $sql_room = mysqli_fetch_assoc($allocate);
                                              $Room_title = $sql_room['title'];

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                              $sql_site = mysqli_fetch_assoc($allocate);
                                              $Site_title = $sql_site['title'];

                                              //get his category title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_id' ") or die(mysqli_error($conn_1));
                                              $sql_category = mysqli_fetch_assoc($allocate);
                                              $Cat_title = $sql_category['title'];

                                              echo '

          												<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
          															<div class="list-group">
          																<ul class="list-group">
          																	<li class="list-group-item list-group-item-success">
          																		<h3>My Bed Space Information/ Make Payment</h3>
          																	</li>
          																</ul>



          															</div>


          												</div>

          												<div class="panel panel-default">
          													<div class="panel-heading">
          														<h3 class="panel-title" style="font-size:20px; font-weight:bold;">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
          													</div>
          													<div class="panel-body">';

                                                      echo '
          														<br>
          														<table width="100%" class="table">
          															<tr>
          																<td width="10%">' . $pic . '</td>
          																<td width="90%">
          																	<table width="100%">
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/>


          																			</td>
          																		</tr>
          																	</table>
          																	<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payment" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
          																</td>
          															</tr>

          														</table><hr/>';

                                          }

                                      }
                                  }

                                  break;
                              } else {

                                  $room_title = "!No accommodation at the moment, Check back much later";

                                  $output = '<br><br>



            										<div class="alert alert-danger">
            										  ' . strtoupper($room_title) . '
            										</div>';

                              }

                          }

                      }

                  } else {
                      $room_title = "no available room yet, check back later!";

                      $output = '<br><br>



  								<div class="alert alert-danger">
  								  ' . strtoupper($room_title) . '
  								</div>


  		        ';
                  }

              }
          }

      }
    }else if($type_id== 4)
    {
      //get user's details
      $q = "SELECT id, CONCAT_WS(' ', first_name, other_names) as name, `phone_no`, `gender`,`number`,
      `email`,`level`,`programme_id`, `image` FROM students_udus WHERE id='$student_id'";
      $r = mysqli_query($conn_2, $q);
      //in the table below result_table stands for resullt fetch throug seraching table(ajax)

      if (mysqli_num_rows($r) > 0) { //show table

          while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
          //get programme Title

          {
              $gender = $row['gender'];
              if($gender=='M'){
                $gender = 'Male';
              }else{
                $gender = 'Female';
              }
              //QuickHashIntHash
              $img = $row['image'];

              if ($img == "") {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              } else {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              }
              //get hostel block

              //get hostel block
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_blocks WHERE campus_id = '$iddd' and gender = '$gender' AND (id !='3' OR id !='4' OR id !='5' OR id !='6') ORDER BY RAND() ") or die(mysqli_error($conn_1));

              while ($ok = mysqli_fetch_assoc($result)) {

                  $title = $ok['title'];
                  $site_id = $ok['campus_id'];
                  $cat_id = $ok['id'];
                  $total_room = $ok['bed_space'];
                  if ($title == "") {
                      $title = "no available hostel block yet";
                  } else {
                      $title == $title;
                  }

                  // checking if available free block SPACE
                  $count_Cat = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE block_id='$cat_id' AND status='1' ");
                  if ($count_Cat) {
                      $sql_row_cat = mysqli_num_rows($count_Cat);

                      if ($sql_row_cat < $total_room) {
                          $category_id = $cat_id;
                          break;
                      } else {
                          $category_id = "";

                      }
                  }
              }

  			//get hostel room
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_rooms WHERE block_id ='$category_id' AND campus_id='$site_id' AND reserved='0' ORDER BY RAND() ") or die(mysqli_error($conn_1));
              if ($result) {
  				$num_rooms = mysqli_num_rows($result);

                  if ($num_rooms > 0) {

                      while ($okk = mysqli_fetch_assoc($result)) {
                          $room_id = $okk['id'];
                          $room_title = $okk['title'];
                          $total_bedspace = $okk['bed_space'];

                          $count = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE room_id='$room_id' AND status='1' ");
                          if ($count) {
                              $sql_row_room = mysqli_num_rows($count);
                              if ($sql_row_room < $total_bedspace) {
                                  $room_id == $room_id;

                                  $chk = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE student_id='$student_id' AND status='1' AND type_id='$type_id' ") or die(mysqli_error($conn_1));
                                  if ($chk) {

                                      $sql_row = mysqli_num_rows($chk);
                                      if ($sql_row > 0) {
                                          $vv = mysqli_fetch_assoc($chk);
                                          $cat_idd = $vv['block_id'];
                                          $room_idd = $vv['room_id'];

                                          //get his room title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_idd' ") or die(mysqli_error($conn_1));
                                          $sql_room = mysqli_fetch_assoc($allocate);
                                          $Room_title = $sql_room['title'];

                                          //get his room title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                          $sql_site = mysqli_fetch_assoc($allocate);
                                          $Site_title = $sql_site['title'];

                                          //get his category title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_idd' ") or die(mysqli_error($conn_1));
                                          $sql_category = mysqli_fetch_assoc($allocate);
                                          $Cat_title = $sql_category['title'];

                                          echo '
              										<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
              											<div class="list-group">
              												<ul class="list-group">
              													<li class="list-group-item list-group-item-success">
              														<h3>My Bed Space Information/ Make Payment</h3>
              													</li>
              												</ul>
              											</div>
              										</div>

              										<div class="panel panel-default">
              											<div class="panel-heading">
              												<h3 class="panel-title">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
              											</div>
              											<div class="panel-body">';

                                                      echo '
              										<label class="alert alert-danger">
              											You have already been allocated room, proceed to payment please!
              										</label><br>
              										<table width="100%" class="table">
              											<tr>
              												<td width="10%">' . $pic . '</td>
              												<td width="90%">
              												<table width="100%">
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/></td>
              													</tr>
              												</table>
              												<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payments" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
              											</td>
              										</tr>

              									</table><hr/>';

                                      } else {
                                          //insert into hostel allocation table

                                          $insert = mysqli_query($conn_1, "INSERT INTO hostel_allocation(school_id,type_id,campus_id,block_id,room_id,student_id,`status`,chk_in_date_time, `session_id`,`added_by`)
  												                VALUES('1','$type_id','$iddd','$cat_id','$room_id','$student_id','1',NOW(), '$session','$student_id')") or die(mysql_error($conn_1));
                                          if ($insert) {
                                              mysqli_query($conn_2, "UPDATE students_udus SET hostel_allocation = '1' WHERE id='$student_id' ");

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_id' ") or die(mysqli_error($conn_1));
                                              $sql_room = mysqli_fetch_assoc($allocate);
                                              $Room_title = $sql_room['title'];

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                              $sql_site = mysqli_fetch_assoc($allocate);
                                              $Site_title = $sql_site['title'];

                                              //get his category title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_id' ") or die(mysqli_error($conn_1));
                                              $sql_category = mysqli_fetch_assoc($allocate);
                                              $Cat_title = $sql_category['title'];

                                              echo '

          												<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
          															<div class="list-group">
          																<ul class="list-group">
          																	<li class="list-group-item list-group-item-success">
          																		<h3>My Bed Space Information/ Make Payment</h3>
          																	</li>
          																</ul>



          															</div>


          												</div>

          												<div class="panel panel-default">
          													<div class="panel-heading">
          														<h3 class="panel-title" style="font-size:20px; font-weight:bold;">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
          													</div>
          													<div class="panel-body">';

                                                      echo '
          														<br>
          														<table width="100%" class="table">
          															<tr>
          																<td width="10%">' . $pic . '</td>
          																<td width="90%">
          																	<table width="100%">
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/>


          																			</td>
          																		</tr>
          																	</table>
          																	<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payment" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
          																</td>
          															</tr>

          														</table><hr/>';

                                          }

                                      }
                                  }

                                  break;
                              } else {

                                  $room_title = "!No accommodation at the moment, Check back much later";

                                  $output = '<br><br>



            										<div class="alert alert-danger">
            										  ' . strtoupper($room_title) . '
            										</div>';

                              }

                          }

                      }

                  } else {
                      $room_title = "no available room yet, check back later!";

                      $output = '<br><br>



  								<div class="alert alert-danger">
  								  ' . strtoupper($room_title) . '
  								</div>


  		        ';
                  }

              }
          }

      }
    }else if($type_id== 5)
    {
      //get user's details
      $q = "SELECT id, CONCAT_WS(' ', first_name, other_names) as name, `phone_no`, `gender`,`number`,
      `email`,`level`,`programme_id`, `image` FROM summer_students WHERE id='$student_id'";
      $r = mysqli_query($conn_1, $q);
      //in the table below result_table stands for resullt fetch throug seraching table(ajax)

      if (mysqli_num_rows($r) > 0) { //show table

          while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
          //get programme Title

          {
              $gender = $row['gender'];
              if($gender=='M'){
                $gender = 'Male';
              }else{
                $gender = 'Female';
              }

              //QuickHashIntHash
              $img = $row['image'];

              if ($img == "") {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              } else {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              }
              //get hostel block

              //get hostel block
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_blocks WHERE campus_id = '$iddd' and gender = '$gender' AND (id !='3' OR id !='4' OR id !='5' OR id !='6') ORDER BY RAND() ") or die(mysqli_error($conn_1));

              while ($ok = mysqli_fetch_assoc($result)) {

                  $title = $ok['title'];
                  $site_id = $ok['campus_id'];
                  $cat_id = $ok['id'];
                  $total_room = $ok['bed_space'];
                  if ($title == "") {
                      $title = "no available hostel block yet";
                  } else {
                      $title == $title;
                  }

                  // checking if available free block SPACE
                  $count_Cat = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE block_id='$cat_id' AND status='1' ");
                  if ($count_Cat) {
                      $sql_row_cat = mysqli_num_rows($count_Cat);

                      if ($sql_row_cat < $total_room) {
                          $category_id = $cat_id;
                          break;
                      } else {
                          $category_id = "";

                      }
                  }
              }

  			//get hostel room
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_rooms WHERE block_id ='$category_id' AND campus_id='$site_id' AND reserved='0' ORDER BY RAND() ") or die(mysqli_error($conn_1));
              if ($result) {
  				$num_rooms = mysqli_num_rows($result);

                  if ($num_rooms > 0) {

                      while ($okk = mysqli_fetch_assoc($result)) {
                          $room_id = $okk['id'];
                          $room_title = $okk['title'];
                          $total_bedspace = $okk['bed_space'];

                          $count = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE room_id='$room_id' AND status='1' ");
                          if ($count) {
                              $sql_row_room = mysqli_num_rows($count);
                              if ($sql_row_room < $total_bedspace) {
                                  $room_id == $room_id;

                                  $chk = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE student_id='$student_id' AND status='1' ") or die(mysqli_error($conn_1));
                                  if ($chk) {

                                      $sql_row = mysqli_num_rows($chk);
                                      if ($sql_row > 0) {
                                          $vv = mysqli_fetch_assoc($chk);
                                          $cat_idd = $vv['block_id'];
                                          $room_idd = $vv['room_id'];

                                          //get his room title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_idd' ") or die(mysqli_error($conn_1));
                                          $sql_room = mysqli_fetch_assoc($allocate);
                                          $Room_title = $sql_room['title'];

                                          //get his room title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                          $sql_site = mysqli_fetch_assoc($allocate);
                                          $Site_title = $sql_site['title'];

                                          //get his category title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_idd' ") or die(mysqli_error($conn_1));
                                          $sql_category = mysqli_fetch_assoc($allocate);
                                          $Cat_title = $sql_category['title'];

                                          echo '
              										<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
              											<div class="list-group">
              												<ul class="list-group">
              													<li class="list-group-item list-group-item-success">
              														<h3>My Bed Space Information/ Make Payment</h3>
              													</li>
              												</ul>
              											</div>
              										</div>

              										<div class="panel panel-default">
              											<div class="panel-heading">
              												<h3 class="panel-title">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
              											</div>
              											<div class="panel-body">';

                                                      echo '
              										<label class="alert alert-danger">
              											You have already been allocated room, proceed to payment please!
              										</label><br>
              										<table width="100%" class="table">
              											<tr>
              												<td width="10%">' . $pic . '</td>
              												<td width="90%">
              												<table width="100%">
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/></td>
              													</tr>
              												</table>
              												<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payments" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
              											</td>
              										</tr>

              									</table><hr/>';

                                      } else {
                                          //insert into hostel allocation table

                                          $insert = mysqli_query($conn_1, "INSERT INTO hostel_allocation(school_id,type_id,campus_id,block_id,room_id,student_id,`status`,chk_in_date_time, `session_id`,`added_by`)
  												                VALUES('1','$type_id','$iddd','$cat_id','$room_id','$student_id','1',NOW(), '$session','$student_id')") or die(mysql_error($conn_1));
                                          if ($insert) {
                                              mysqli_query($conn_1, "UPDATE summer_students SET hostel_allocation = '1' WHERE id='$student_id' ");

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_id' ") or die(mysqli_error($conn_1));
                                              $sql_room = mysqli_fetch_assoc($allocate);
                                              $Room_title = $sql_room['title'];

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                              $sql_site = mysqli_fetch_assoc($allocate);
                                              $Site_title = $sql_site['title'];

                                              //get his category title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_id' ") or die(mysqli_error($conn_1));
                                              $sql_category = mysqli_fetch_assoc($allocate);
                                              $Cat_title = $sql_category['title'];

                                              echo '

          												<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
          															<div class="list-group">
          																<ul class="list-group">
          																	<li class="list-group-item list-group-item-success">
          																		<h3>My Bed Space Information/ Make Payment</h3>
          																	</li>
          																</ul>



          															</div>


          												</div>

          												<div class="panel panel-default">
          													<div class="panel-heading">
          														<h3 class="panel-title" style="font-size:20px; font-weight:bold;">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
          													</div>
          													<div class="panel-body">';

                                                      echo '
          														<br>
          														<table width="100%" class="table">
          															<tr>
          																<td width="10%">' . $pic . '</td>
          																<td width="90%">
          																	<table width="100%">
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/>


          																			</td>
          																		</tr>
          																	</table>
          																	<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payment" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
          																</td>
          															</tr>

          														</table><hr/>';

                                          }

                                      }
                                  }

                                  break;
                              } else {

                                  $room_title = "!No accommodation at the moment, Check back much later";

                                  $output = '<br><br>



            										<div class="alert alert-danger">
            										  ' . strtoupper($room_title) . '
            										</div>';

                              }

                          }

                      }

                  } else {
                      $room_title = "no available room yet, check back later!";

                      $output = '<br><br>



  								<div class="alert alert-danger">
  								  ' . strtoupper($room_title) . '
  								</div>


  		        ';
                  }

              }
          }

      }
    }else if($type_id== 6)
    {
      //get user's details
      $q = "SELECT id, CONCAT_WS(' ', first_name, other_names) as name, `phone_no`, `gender`,`number`,
      `email`,`level`,`programme_id`, `image` FROM students_lvsp WHERE id='$student_id'";
      $r = mysqli_query($conn_1, $q);
      //in the table below result_table stands for resullt fetch throug seraching table(ajax)

      if (mysqli_num_rows($r) > 0) { //show table

          while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
          //get programme Title

          {
              $gender = $row['gender'];
              if($gender=='M'){
                $gender = 'Male';
              }else{
                $gender = 'Female';
              }

              //QuickHashIntHash
              $img = $row['image'];

              if ($img == "") {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              } else {

                  $pic = '<img src="../../images/user.jpg" width="120px" height="130px" class="img-circle"/>';
              }
              //get hostel block

              //get hostel block
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_blocks WHERE campus_id = '$iddd' and gender = '$gender' AND (id !='3' OR id !='4' OR id !='5' OR id !='6') ORDER BY RAND() ") or die(mysqli_error($conn_1));

              while ($ok = mysqli_fetch_assoc($result)) {

                  $title = $ok['title'];
                  $site_id = $ok['campus_id'];
                  $cat_id = $ok['id'];
                  $total_room = $ok['bed_space'];
                  if ($title == "") {
                      $title = "no available hostel block yet";
                  } else {
                      $title == $title;
                  }

                  // checking if available free block SPACE
                  $count_Cat = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE block_id='$cat_id' AND status='1' ");
                  if ($count_Cat) {
                      $sql_row_cat = mysqli_num_rows($count_Cat);

                      if ($sql_row_cat < $total_room) {
                          $category_id = $cat_id;
                          break;
                      } else {
                          $category_id = "";

                      }
                  }
              }

  			//get hostel room
              $result = mysqli_query($conn_1, "SELECT * FROM hostel_rooms WHERE block_id ='$category_id' AND campus_id='$site_id' AND reserved='0' ORDER BY RAND() ") or die(mysqli_error($conn_1));
              if ($result) {
  				$num_rooms = mysqli_num_rows($result);

                  if ($num_rooms > 0) {

                      while ($okk = mysqli_fetch_assoc($result)) {
                          $room_id = $okk['id'];
                          $room_title = $okk['title'];
                          $total_bedspace = $okk['bed_space'];

                          $count = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE room_id='$room_id' AND status='1' ");
                          if ($count) {
                              $sql_row_room = mysqli_num_rows($count);
                              if ($sql_row_room < $total_bedspace) {
                                  $room_id == $room_id;

                                  $chk = mysqli_query($conn_1, "SELECT * FROM hostel_allocation WHERE student_id='$student_id' AND status='1' ") or die(mysqli_error($conn_1));
                                  if ($chk) {

                                      $sql_row = mysqli_num_rows($chk);
                                      if ($sql_row > 0) {
                                          $vv = mysqli_fetch_assoc($chk);
                                          $cat_idd = $vv['block_id'];
                                          $room_idd = $vv['room_id'];

                                          //get his room title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_idd' ") or die(mysqli_error($conn_1));
                                          $sql_room = mysqli_fetch_assoc($allocate);
                                          $Room_title = $sql_room['title'];

                                          //get his room title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                          $sql_site = mysqli_fetch_assoc($allocate);
                                          $Site_title = $sql_site['title'];

                                          //get his category title
                                          $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_idd' ") or die(mysqli_error($conn_1));
                                          $sql_category = mysqli_fetch_assoc($allocate);
                                          $Cat_title = $sql_category['title'];

                                          echo '
              										<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
              											<div class="list-group">
              												<ul class="list-group">
              													<li class="list-group-item list-group-item-success">
              														<h3>My Bed Space Information/ Make Payment</h3>
              													</li>
              												</ul>
              											</div>
              										</div>

              										<div class="panel panel-default">
              											<div class="panel-heading">
              												<h3 class="panel-title">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
              											</div>
              											<div class="panel-body">';

                                                      echo '
              										<label class="alert alert-danger">
              											You have already been allocated room, proceed to payment please!
              										</label><br>
              										<table width="100%" class="table">
              											<tr>
              												<td width="10%">' . $pic . '</td>
              												<td width="90%">
              												<table width="100%">
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/></td>
              													</tr>
              													<tr>
              														<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/></td>
              													</tr>
              												</table>
              												<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payments" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
              											</td>
              										</tr>

              									</table><hr/>';

                                      } else {
                                          //insert into hostel allocation table

                                          $insert = mysqli_query($conn_1, "INSERT INTO hostel_allocation(school_id,type_id,campus_id,block_id,room_id,student_id,`status`,chk_in_date_time, `session_id`,`added_by`)
  												                VALUES('1','$type_id','$iddd','$cat_id','$room_id','$student_id','1',NOW(), '$session','$student_id')") or die(mysql_error($conn_1));
                                          if ($insert) {
                                              mysqli_query($conn_1, "UPDATE students_lvsp SET hostel_allocation = '1' WHERE id='$student_id' ");

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_rooms WHERE id='$room_id' ") or die(mysqli_error($conn_1));
                                              $sql_room = mysqli_fetch_assoc($allocate);
                                              $Room_title = $sql_room['title'];

                                              //get his room title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_campuses WHERE id='$iddd' ") or die(mysqli_error($conn_1));
                                              $sql_site = mysqli_fetch_assoc($allocate);
                                              $Site_title = $sql_site['title'];

                                              //get his category title
                                              $allocate = mysqli_query($conn_1, "SELECT  title FROM hostel_blocks WHERE id='$cat_id' ") or die(mysqli_error($conn_1));
                                              $sql_category = mysqli_fetch_assoc($allocate);
                                              $Cat_title = $sql_category['title'];

                                              echo '

          												<div class="col-md-12" style="padding-left:0px; padding-right:0px;">
          															<div class="list-group">
          																<ul class="list-group">
          																	<li class="list-group-item list-group-item-success">
          																		<h3>My Bed Space Information/ Make Payment</h3>
          																	</li>
          																</ul>



          															</div>


          												</div>

          												<div class="panel panel-default">
          													<div class="panel-heading">
          														<h3 class="panel-title" style="font-size:20px; font-weight:bold;">' . strtoupper($Site_title) . ' ------- ' . strtoupper($Cat_title) . ' ------- ' . strtoupper($Room_title) . '</h3>
          													</div>
          													<div class="panel-body">';

                                                      echo '
          														<br>
          														<table width="100%" class="table">
          															<tr>
          																<td width="10%">' . $pic . '</td>
          																<td width="90%">
          																	<table width="100%">
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">NAME: ' . strtoupper($row['name']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Matric Number: ' . strtoupper($row['number']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Phone Number: ' . strtoupper($row['phone_no']) . '</h3><hr/>

          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Email: ' . strtoupper($row['email']) . '</h3><hr/>


          																			</td>
          																		</tr>
          																		<tr>
          																			<td width="100%"><h3 class="label label-default" style="font-size:12px;">Gender: ' . strtoupper($gender) . '</h3><hr/>


          																			</td>
          																		</tr>
          																	</table>
          																	<!-- PROCEED TO PAYMENT --> <a class="btn btn-primary" style="width:80%; margin-top:20px;" href="dashboard.php?sms=payment" data-toggle="tooltip" title="Click here to Proceed to payment" data-placement="top"><i class="glyphicon glyphicon-ok"> </i> PROCEED TO PAYMENT</a>
          																</td>
          															</tr>

          														</table><hr/>';

                                          }

                                      }
                                  }

                                  break;
                              } else {

                                  $room_title = "!No accommodation at the moment, Check back much later";

                                  $output = '<br><br>



            										<div class="alert alert-danger">
            										  ' . strtoupper($room_title) . '
            										</div>';

                              }

                          }

                      }

                  } else {
                      $room_title = "no available room yet, check back later!";

                      $output = '<br><br>



  								<div class="alert alert-danger">
  								  ' . strtoupper($room_title) . '
  								</div>


  		        ';
                  }

              }
          }

      }
    }






    echo $output;

}
