Pagination Using Pagination Class

pagination.php

<?php

// This is a helper class to make paginating  records easy.
class Pagination {
   
  public $current_page;
  public $per_page;
  public $total_count;

  public function __construct($page=1, $per_page=20, $total_count=0){
      $this->current_page = (int)$page;
    $this->per_page = (int)$per_page;
    $this->total_count = (int)$total_count;
  }

  public function offset() {
    // Assuming 20 items per page:
    // page 1 has an offset of 0    (1-1) * 20
    // page 2 has an offset of 20   (2-1) * 20
    //   in other words, page 2 starts with item 21
    return ($this->current_page - 1) * $this->per_page;
  }

  public function total_pages() {
    return ceil($this->total_count/$this->per_page);
    }
   
  public function previous_page() {
    return $this->current_page - 1;
  }
 
  public function next_page() {
    return $this->current_page + 1;
  }

    public function has_previous_page() {
        return $this->previous_page() >= 1 ? true : false;
    }

    public function has_next_page() {
        return $this->next_page() <= $this->total_pages() ? true : false;
    }
}

?>

view_faculty.php

<?php
   require_once("includes/initialize.php");
   include_layout_template("header.php");
   include_layout_template("menu.php");

   $current_page = !empty($_GET["page"]) ? (int) ($_GET["page"]) : 1;
   $per_page = 3;
   $total_count = Teacher::count_all();

   $pagination = new Pagination($current_page, $per_page, $total_count);

?>

 <div class="panel panel-success">
  <div class="panel-heading">
      <div class="panel-title">
       <strong>List of Teachers</strong>
      </div>
  </div>
  <div class="panel-body"> 
     <form class="form-horizontal" id="searchForm" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
       <div class="form-group">
         <label for="department" class="col-sm-2 control-label">Department</label>
         <select name="option" autofocus>
               <option value="ALL" Selected>ALL</option>
               <option value="BA" >BA</option>
               <option value="BSA">BSA</option>
               <option value="CS">CS</option>
          </select>
         <button type="submit" name="search" class="btn btn-primary btn-xs"><span class="glyphicon glyphicon-floppy-search">Search</button>       
       </div>
     </form> 
     <div class="table-responsive">         
    <table class="table">
        <thead>
        <tr>
            <th>#</th>
            <th>ID</th>
            <th>Name</th>
            <th>Gender</th>
            <th>Department</th>
            <th></th>
        </tr>
        </thead>
        <tbody>
       
        <?php
            $teachers = Teacher::find_by_sql("SELECT * FROM teachers limit  {$per_page} offset {$pagination->offset()}");
            if ($_SERVER["REQUEST_METHOD"]=="POST") {
                if (!empty($_POST["option"])) {
                   $option = $_POST["option"];
                   if ($option != "ALL") {
                      $teachers = Teacher::find_by_sql("SELECT * FROM teachers where department='{$option}' limit {$per_page} offset {$pagination->offset()}");
                   }
                }
            }
            else if ($_SERVER["REQUEST_METHOD"]=="GET") {
                if (!empty($_GET["action"])) {
                   $id =  $_GET["id"];
                   $action = $_GET["action"];
                   if ($action == "delete") {
                      $teacher = Teacher::find_by_id($id);
                      if ($teacher) {
                        // log_action("{$_SESSION['username']} | Delete | {$teacher->id}");
                         $teacher->delete();
                         $teachers = Teacher::find_all();
                      }
                   }
                }
            }
           
            foreach ($teachers as $teacher) {
               echo "<tr>";
               echo "<td> <input type='checkbox' value='{$teacher->id}'> </td>";
               echo "<td> {$teacher->id} </td>";
               echo "<td> {$teacher->name} </td>";
               echo "<td> {$teacher->gender} </td>";
               echo "<td> {$teacher->department} </td>";
               echo "<td> <a href='view_faculty.php?id={$teacher->id}&action=delete' class='btn btn-danger'>Delete</a>";
               echo "&nbsp; <a href='teacher_form.php?id={$teacher->id}&action=edit' class='btn btn-primary'>Edit</a> </td>";
               echo "</tr>";
            }
        ?>
       
     </tbody>
    </table>
    <ul class="pagination">
    <?php 
      for ($i = 1; $i <= $pagination->total_pages(); $i++) {
        if ($i == $pagination->current_page)
          echo "<li class='active'><a href='view_faculty.php?page={$i}'>{$i}</a></li>";
        else
          echo "<li><a href='view_faculty.php?page={$i}'>{$i}</a></li>";
      }
    ?>
    </ul>
    <br/>
    <a type="button" class="btn btn-primary" href=teacher_form.php?action=add>Add</a>
    </div>
  </div> 
 </div> 

<?php
   include_layout_template("footer.php");
?>
          




Comments