Pagination extension

The pagination extension can be used to divide the amount of data by navigating through using pagination links. After downloading the extension, add the file inside the extensions folder.

Class: Pagination

In this table you'll find information about the different methods you can use and what kind of data can be passed as an argument.

Method Arg Type Expect
get() 1 array page get key values should be included
get() 2 array data
get() 3 int divided number
getPageNumbers() - - -

Getting paginated data

Fetch data from a table and call the get method. The first argument should be the request variable parameter from the controller function method. The second argument should be the fetched data. The last argument should be the preffered number to divide the data. Finally, call the getPageNumbers function method to get the number of navigation links and pass the data to the view.

<?php
                
  namespace app\controllers;
  
  use extensions\Pagination;
  use database\DB;
                
  class ExampleController {
                
    public function example($request) {    
                 
      $tableData = DB::try()->all('table')->fetch();
      $data['paginatedData'] = Pagination::get($request, $tableData, 10);
      $data['numberOfPaginatedPages'] = Pagination::getPageNumbers();
    
      return $this->view('view')->data($data);
    }              
  }

Show the paginated data and add the navigation

Underneath the table, create some sort of check to determine if there is enough data to paginate. Loop through the paginated number to show the navigation links. Add get parameter values where the key value is set to 'page' and the value to the number of pagination. Finally, add the number of pagination inside the anchor tag to show the number of pagination.

<table>
  <thead>
    <tr>
      <th>Column:</th>
      <th>Column:</th>
      <th>Column:</th>
    </tr>
  </thead>
  <tbody>
    <?php foreach($paginatedData as $data) { ?>
        
      <tr>
         <td><?php echo $data['column'];</td>
        <td><?php echo $data['column'];</td>
        <td><?php echo $data['column'];</td>
      <tr>
     
    <?php } ?>
    
  </tbody>
</table>

<?php if($numberOfPaginatedPages !== null && count($numberOfPaginatedPages) > 1) { ?>

  <?php foreach($numberOfPaginatedPages as $numberOfPaginatedPage) { ?>
  
    <nav>
      <ul>
        <li>
          <a href="?page=<?php echo $numberOfPaginatedPage; ?>"><?php echo $numberOfPaginatedPage; ?></a>
        </li>
      </ul>
    </nav>
      
  <?php } ?>
      
<?php } ?>