AngularJS Pagination with PHP/MySQLi

Getting Started

I've used CDN for Bootstrap and Angular JS in this tutorial so you need internet connection for them to work. I'm going to used an external js for pagination which you can view using this link. This is included in the downloadable of this tutorial.

Creating our Database

First, we're going to create our database and insert sample data. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as angular. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `members` (
  2.   `memid` int(11) NOT NULL AUTO_INCREMENT,
  3.   `firstname` varchar(30) NOT NULL,
  4.   `lastname` varchar(30) NOT NULL,
  5.   `address` text NOT NULL,
  6. PRIMARY KEY(`memid`)
  1. INSERT INTO `members` (`memid`, `firstname`, `lastname`, `address`) VALUES
  2. (1, 'Neovic', 'Devierte', 'Silay City'),
  3. (2, 'Julyn', 'Divinagracia', 'E.B. Magalona'),
  4. (3, 'Gemalyn', 'Cepe', 'Bohol'),
  5. (4, 'Matet', 'Devierte', 'Silay City'),
  6. (5, 'Tintin', 'Devierte', 'Silay City'),
  7. (6, 'Bien', 'Devierte', 'Cebu City'),
  8. (7, 'Cherry', 'Ambayec', 'Cebu City'),
  9. (8, 'Jubilee', 'Limsiaco', 'Silay City'),
  10. (9, 'Janna ', 'Atienza', 'Talisay City'),
  11. (10, 'Desire', 'Osorio', 'Bacolod City'),
  12. (11, 'Debbie', 'Osorio', 'Talisay City'),
  13. (12, 'Nipoy ', 'Polondaya', 'Victorias City'),
  14. (13, 'Johnedel', 'Balino', 'Cauyan, Negros'),
  15. (14, 'Nereca', 'Tajonera', 'Cauayan, Negros'),
  16. (15, 'Jerome', 'Robles', 'Cebu City');
database sql

index.php

Next, we're going to create our index which contains the table that we are going to paginate.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="app">
  3.     <meta charset="utf-8">
  4.     <title>AngularJS Pagination with PHP/MySQLi</title>
  5.     <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6.     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  7. </head>
  8. <body ng-controller="memberdata">
  9. <div class="container">
  10.     <h1 class="page-header text-center">AngularJS Pagination with PHP/MySQLi</h1>
  11.     <div class="row">
  12.                 <div class="col-md-8 col-md-offset-2">
  13.                         <table class="table table-bordered table-striped" style="margin-top:10px;">
  14.                                 <thead>
  15.                     <tr>
  16.                         <th>Member ID</th>
  17.                         <th>Firstname</th>
  18.                         <th>Lastname</th>
  19.                         <th>Address</th>
  20.                     </tr>
  21.                 </thead>
  22.                                 <tbody>
  23.                                         <tr dir-paginate="member in members|itemsPerPage:5">
  24.                                                 <td>{{ member.memid }}</td>
  25.                                                 <td>{{ member.firstname }}</td>
  26.                                                 <td>{{ member.lastname }}</td>
  27.                                                 <td>{{ member.address }}</td>
  28.                                         </tr>
  29.                                 </tbody>
  30.                         </table>
  31.                         <div class="pull-right" style="margin-top:-30px;">
  32.                         <dir-pagination-controls
  33.                            max-size="5"
  34.                            direction-links="true"
  35.                            boundary-links="true" >
  36.                         </dir-pagination-controls>
  37.                         </div>
  38.                 </div>
  39.         </div>
  40. </div>
  41. <script src="dirPaginate.js"></script>
  42. <script src="angular.js"></script>
  43. </body>
  44. </html>
Notice that in our tbody in tr we have replace ng-repeat with dir-paginate and we have added itemsPerPage:5 which indicates the number of items and you can edit it depending on the number of items you want to display per page. max-size in dir-pagination-controls refers to the max number of buttons of pagination.

angular.js

This is our angular.js code which contains the script in fetching data from our API.
  1. var app = angular.module('app', ['angularUtils.directives.dirPagination']);
  2. app.controller('memberdata',function($scope, $http){
  3.     $http.get("fetch.php").success(function(data){
  4.         $scope.members = data;
  5.     });
  6.    
  7. });
Notice that we have included 'angularUtils.directives.dirPagination' in our module. This is the dependency of the external js for pagination that we have included.

fetch.php

Lastly, this is our PHP API that fetches data from our MySQL Table.
  1. <?php
  2.         $conn = new mysqli('localhost', 'root', '', 'angular');
  3.         $output = array();
  4.         $sql = "SELECT * FROM members";
  5.         $query=$conn->query($sql);
  6.         while($row=$query->fetch_array()){
  7.                 $output[] = $row;
  8.         }
  9.  
  10.         echo json_encode($output);
  11. ?>
That ends this tutorial. Happy Coding :)

Add new comment