article

Friday, December 24, 2021

AngularJS PHP Mysql Pagination

AngularJS PHP Mysql Pagination

angularjs CDN https://angularjs.org/ Version 1.8.2 https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js

https://github.com/michaelbromley/angularUtils/tree/master/src/directives/pagination

dirPagination libs Its very simple and easy to use

Database Table

CREATE TABLE `members` (
  `id` int(11) NOT NULL,
  `firstname` varchar(30) NOT NULL,
  `lastname` varchar(30) NOT NULL,
  `address` varchar(150) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `members` (`id`, `firstname`, `lastname`, `address`) VALUES
(1, 'Airi ', 'Satou', 'Tokyo'),
(2, 'Angelica ', 'Ramos', 'London'),
(3, 'Ashton ', 'Cox', 'San Francisco'),
(4, 'Bradley ', 'Greer', 'London'),
(5, 'Brenden ', 'Wagner', 'San Francisco'),
(6, 'Brielle', 'Williamson', 'New York'),
(7, 'Bruno', 'Nash', 'London'),
(8, 'Caesar', 'Vance', 'New York'),
(9, 'Cara', 'Stevens', 'New York'),
(10, 'Cedric', 'Kelly', 'Edinburgh');

ALTER TABLE `members`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `members`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;
index.html
//index.html
<!DOCTYPE html>
<html>
<head>
<title>AngularJS PHP Mysql Pagination</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="dirPaginate.js"></script>
</head>
<body>
<div class="container" ng-app="paginationApp" ng-controller="paginationController">
   <br />
    <h3 align="center">AngularJS PHP Mysql Pagination </h3>
    <br />
    <div class="table-responsive">
    <table class="table table-striped table-bordered">
		<thead>
		  <tr>
		   <th width="50">ID</th>
		   <th>Firts Name</th>
		   <th>Last Name</th>
		   <th>Address</th>
		  </tr>
		</thead>
		<tbody>
		  <tr dir-paginate="rs in allData|itemsPerPage:5">
		   <td>{{ rs.id }}</td>
		   <td>{{ rs.firstname }}</td>
		   <td>{{ rs.lastname }}</td>
		   <td>{{ rs.address }}</td>
		  </tr>
		</tbody>
    </table>
    </div>
    <div align="right">
		<dir-pagination-controls max-size="5" direction-links="true" boundary-links="true" >
		</dir-pagination-controls>
    </div>
</div>
<script>
var pagination_app = angular.module('paginationApp', ['angularUtils.directives.dirPagination']);
	pagination_app.controller('paginationController', function($scope, $http){
        $http({
            method: 'GET',
            url: 'fetch.php',
        }).then(function (data){
            console.log(data)
            $scope.allData = data.data;
        },function (error){
            console.log(error, 'can not get data.');
        });
	});
</script>
</body>
</html>
fetch.php
//fetch.php
<?php
$connect = new PDO("mysql:host=localhost;dbname=testingdb", "root", "");
$query = "SELECT * FROM members ORDER BY id DESC";
$statement = $connect->prepare($query);
if($statement->execute())
{
	while($row = $statement->fetch(PDO::FETCH_ASSOC))
		{
		  $data[] = $row;
		}
	echo json_encode($data);
}
?>

Related Post