In this tutorial, we will create a Simple Pagination With MySQLi Using AngularJS. This code will paginate the data in the database by applying AngularJS directives. The AngularJS will process a request to fetch the data in the database server to display it in the HTML table. This is a user-friendly program you can apply and modify this program to your system.
We will be using AngularJS as a comprehensive JavaScript framework that extends the browser capabilities. It handles the heavy lifting of DOM manipulation and AJAX request as a whole.
Getting Started:
First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html.
And this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
Lastly, this is the link for the AngularJS https://angularjs.org/.
Creating Database
Open your database web server then create a database name in it db_page, after that, click Import then locates the database file inside the folder of the application then click ok.
Creating the database connection
Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
<?php $conn = mysqli_connect("localhost", "root", "", "db_update"); if(!$conn){ die("Error: Failed to connect to database!"); } ?>
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.
<!DOCTYPE html> <html lang="en" ng-app="myModule"> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> </head> <body ng-controller="myController"> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-primary">PHP - Simple Pagination With MySQLi Using AngularJS</h3> <hr style="border-top:1px dotted #ccc;"/> <div class="col-md-2"></div> <div class="col-md-8"> <table class="table table-bordered"> <thead class="alert-info"> <tr> <th ng-click="sort('firstname')">Firstname <span class="glyphicon sort-icon" ng-show="sortKey=='firstname'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span> </th> <th ng-click="sort('lastname')">Lastname <span class="glyphicon sort-icon" ng-show="sortKey=='lastname'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span> </th> <th ng-click="sort('gender')">Gender <span class="glyphicon sort-icon" ng-show="sortKey=='gender'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span> </th> <th ng-click="sort('address')">Address <span class="glyphicon sort-icon" ng-show="sortKey=='address'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span> </th> </tr> </thead> <tbody> <tr dir-paginate="member in members|orderBy:'lastname'|orderBy:sortKey:reverse|itemsPerPage:5"> <td>{{member.firstname}}</td> <td>{{member.lastname}}</td> <td>{{member.gender}}</td> <td>{{member.address}}</td> </tr> </tbody> </table> <dir-pagination-controls direction-links="true" boundary-links="true" > </dir-pagination-controls> </div> </div>
Creating the Main Function
This code contains the main function of the application. This code will paginate the data using AngularJS directives. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below.
data.php
<?php require_once 'conn.php'; $data = array(); $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error()); while($fetch = mysqli_fetch_array($query)){ $data[] = $fetch; } echo json_encode($data); ?>
script.js
Note: Make sure you save this file inside the js directory in order the script works.
var app = angular.module('myModule', ['angularUtils.directives.dirPagination']); app.controller('myController', function($scope, $http){ $http.get('data.php').then(function(response){ $scope.members = response.data; }); $scope.sort = function(keyword){ $scope.sortKey = keyword; $scope.reverse = !$scope.reverse; } });
There you have it we successfully created a Simple Pagination With MySQLi using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!