如何将PHP代码与html分开,代码仍可正常工作! 并在选择菜单上方打印$ selection错误变量

I want to print the the $selection_error variable on top of the selection menu but it is not working there but if I print the $selection_error variable below the table it is working. Is there any way to separate the php code from html by placing it on top of all the html code so that I could print the $selection_error variable on top of the selection menu?

I'm trying to add this code below the h3 tag above the form..

<?php if (isset($selection_error))  
        print_r("<div class='alert alert-danger' role='alert'>
              $selection_error <button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button> </div>");
?>

not exactly below the table as it is shown in the code!

<?php include("includes/header.php"); ?>
<div id="wrapper">
  <!-- Navigation -->
  <?php include("includes/navbar.php"); ?>
  <!--./Navigation  -->
  <div id="page-wrapper">
    <div class="container-fluid">
      <!-- Page Heading -->
      <div class="row">
        <div class="col-lg-12">
          <h3 class="page-header">
             <div class="text-center" style="letter-spacing: 1.5px;">
                View Student Information
             </div>
          </h3>        
          <div class="formstyle">
            <form class="form-inline st" action="" method="POST">
              <label for="sel11" class="form-control-label ab">Select Semester</label>

<?php
$query = $connection->query("SELECT * FROM semester ORDER BY sem_id ASC");
$rowCount = $query->num_rows;
?>

                <select class="form-control" id="sel11" name="sel11">
                  <option selected="selected" disabled="disabled">Select</option>
<?php 
if($rowCount > 0){
  while($row = $query->fetch_assoc()){ 
      echo '<option value="'.$row['sem_id'].'">'.$row['sem_name'].'</option>';
  }
}
else {
  echo '<option value="">Semester not available</option>';
}
?>
                </select>                  
              <label for="sel12" class="form-control-label ab">Start Session</label>

<?php
$query = $connection->query("SELECT DISTINCT(session_start) FROM student ORDER BY session_start ASC");
$rowCount = $query->num_rows;
?>

                <select class="form-control" id="sel12" name="sel12">
                  <option selected="selected" disabled="disabled">Select</option>
<?php 
if($rowCount > 0){
  while($row = $query->fetch_assoc()){ 
      echo '<option value="'.$row['session_start'].'">'.$row['session_start'].'</option>';
  }
}
else {
  echo '<option value="">Semester not available</option>';
}
?>              
                </select>                  
              <label for="sel13" class="form-control-label ab">End Session</label>

<?php
$query = $connection->query("SELECT DISTINCT(session_end) FROM student ORDER BY session_end ASC");
$rowCount = $query->num_rows;
?>

                <select class="form-control" id="sel13" name="sel13">
                  <option selected="selected" disabled="disabled">Select</option>
<?php 
if($rowCount > 0){
  while($row = $query->fetch_assoc()){ 
      echo '<option value="'.$row['session_end'].'">'.$row['session_end'].'</option>';
  }
}
else {
  echo '<option value="">Semester not available</option>';
}
?>
                </select>
                <div class="ab inline">
                  <button type="submit" class="btn btn-primary" name="submit">Submit</button> 
                </div>
            </form>               
          </div>          
          <input type="text" id="myInput" onkeyup="myFunction2()" placeholder="Search for roll no or names..">
            <table class="table table-bordered table-hover" id="myTable">
              <thead>
                <tr>
                  <th scope="col">#</th>
                  <th scope="col">Roll No</th>
                  <th scope="col">Name</th>
                  <th scope="col">Email</th>
                  <th scope="col">Start Session</th>
                  <th scope="col">End Session</th>
                  <th scope="col">Semester</th>
                  <th scope="col">Action</th>
                </tr>
              </thead>
              <tbody>

<?php 
$error = false;
if(isset($_POST['submit'])) {

  if(isset($_POST['sel11']) && isset($_POST['sel12']) && isset($_POST['sel13'])){

    $sel11 = mysqli_real_escape_string($connection, $_POST['sel11']);
    $sel12 = mysqli_real_escape_string($connection, $_POST['sel12']);
    $sel13 = mysqli_real_escape_string($connection, $_POST['sel13']);


    $query = "SELECT * FROM student, semester WHERE student.session_start = '{$sel12}' AND student.session_end = '{$sel13}' AND student.sem_id = semester.sem_id AND student.sem_id = '{$sel11}' ORDER BY student.s_rollno ASC";
    $select_student_query = mysqli_query($connection, $query);

    $rowCount = $select_student_query->num_rows;

    if($rowCount > 0){
      $i = 0;
      while ($row = mysqli_fetch_assoc($select_student_query)) {
        $s_id = $row['s_id'];
        $s_rollno = $row['s_rollno'];
        $s_email = $row['s_email'];
        $s_name = $row['s_name'];
        $session_start = $row['session_start'];
        $session_end = $row['session_end'];
        $sem_name =  $row['sem_name'];

        $i = $i + 1;

        echo "<tr>";
          echo "<th scope='row'>$i</th>";
          echo "<td>$s_rollno</td>";
          echo "<td>$s_name</td>";
          echo "<td>$s_email</td>";
          echo "<td>$session_start</td>";
          echo "<td>$session_end</td>";
          echo "<td>$sem_name</td>";
          echo "<td>
              <div class='btn-group'>
                <a href='updateStudent.php?update={$s_id}' class='btn btn-warning' role='button'>Edit</a>
                <a href='viewStudentInfo.php?delete={$s_id}' class='btn btn-danger' role='button'>Delete</a>
              </div>
          </td>";
        echo "</tr>";
      }
    }
    else {
      echo "<tr>";
        echo "<td class='text-center' colspan='8'>";
            echo "<h3 class='text-danger'>No Data Found!</h3>";
        echo "</td>";
      echo "</tr>";

    }
  }
  else {
    $error = true;
    $selection_error = "Select at least one option from each dropdown list!";    
  }
}
 ?> 
              </tbody>
            </table>   

<?php if (isset($selection_error))  
        print_r("<div class='alert alert-danger' role='alert'>
              $selection_error <button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button> </div>");
?> 

<?php 

if(isset($_GET['delete'])) {
  $s_id = $_GET['delete'];

  $query = "DELETE FROM student WHERE s_id = '{$s_id}'";
  $delete_student_query = mysqli_query($connection, $query);

   header("Location: viewStudentInfo.php");

}

 ?>              
        </div>
      </div>
      <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
  </div>
  <!-- /#page-wrapper -->
  <?php include("includes/footer.php"); ?>