Showing posts with label php-pagination. Show all posts
Showing posts with label php-pagination. Show all posts

Thursday, 21 June 2018

PHP Mysql Pagination using AngularJS



If you are looking for AngularJS tutorial on paginatin then in this post we have describe how to make pagination by using AngularJS with PHP. If we have working on any dynamic website by using PHP then for display mysql table data on web page in tabular format then we would required pagination. Because by using Pagination, it will divided large amount of data into different part and it will load only some number of rows on web page and suppose we want to see next data then we have to click on next link button, so when we have click on link then it has send request for fetch next data and display on web page in tabular format without refresh of web page. So by using Pagination we have reduce server load, because if we have not use pagination then it will fetch all data from database at the same time and it will reduce our website speed also. But by using pagination it will load only some rows of data and it will load next data as pere requirement.

Pagination is a required feature in web development and this feature we want to make by using AngularJS with PHP. For make pagination by using AngularJS with PHP, for this we have use "dirPagination" AngularJS module, this module is for create pagination with AngularJS. By using this module we can easily pagination by using AngularJS with any dynamic language and it has reduce our server side code and it has directly make pagination link. For make pagination by using this AngularJS module we have to just send data in JSON string format then it will automatically convert this data into pagination with pagination link. We have to just define how many data we have display per page in "itemsPerPage" option of "dir-paginate" directoyr. This is directory work as same like ng-repeat directory. By using this directory we can print data on web page but this directory has been make by this AngularJS module.

So, if you are using AngularJS as front-end and PHP as backend and we want to make pagination in your web application then this tutorial will helpful. Because in this post we have describe PHP pagination using AngularJS. Below you can find source code of Pagination using AngularJS with PHP.










Source Code


index.php



<?php
//index.php

?>
<!DOCTYPE html>
<html>
 <head>
  <title>PHP Mysql Pagination using AngularJS</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
        <script src="dirPaginate.js"></script>
 </head>
 <body>
  <div class="container" ng-app="paginationApp" ng-controller="paginationController">
   <br />
   <h3 align="center">PHP Mysql Pagination using AngularJS</h3>
   <br />
   <div class="table-responsive">
    <table class="table table-striped table-bordered">
     <thead>
      <tr>
       <th>Student Name</th>
       <th>Student Phone</th>
      </tr>
     </thead>
     <tbody>
      <tr dir-paginate="singleData in allData|itemsPerPage:10">
       <td>{{ singleData.student_name }}</td>
       <td>{{ singleData.student_phone }}</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>
 </body>
</html>

<script>
var pagination_app = angular.module('paginationApp', ['angularUtils.directives.dirPagination']);
pagination_app.controller('paginationController', function($scope, $http){
 $http.get('fetch.php').success(function(data){
  $scope.allData = data;
 });
});
</script>


fetch.php



<?php

//fetch.php

$connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");
$query = "SELECT * FROM tbl_student ORDER BY student_id DESC";
$statement = $connect->prepare($query);
if($statement->execute())
{
 while($row = $statement->fetch(PDO::FETCH_ASSOC))
 {
  $data[] = $row;
 }
 echo json_encode($data);
}

?>





Tuesday, 24 May 2016

How to Make Simple Pagination using PHP MySql


Are you searching for simple PHP Pagination for a starting level of php programmer then you are at the correct place and This PHP pagination script or example is very well helpful to you. By using this PHP Script for Create Pagination with Previous and Next page link, First and Last page link also. Pagination is a style to make partition between content into different pages. Here we can define every pages as a separate URL. By click on that URL or number of that page, user can visit the content of that Page. For every page has number which is in incremental Page number. You can use this simple PHP pagination script or PHP pagination code into your project. For making simple pagination We have used PHP programming code with mysqli. We Hope with this PHP Pagination Tutorial using PHP MYSQL will be helpful to you to get the simple learning about the pagination as a beginner in php.



Source Code


pagination.php



<?php
$connect = mysqli_connect("localhost", "root", "", "testing");
$record_per_page = 5;
$page = '';
if(isset($_GET["page"]))
{
 $page = $_GET["page"];
}
else
{
 $page = 1;
}

$start_from = ($page-1)*$record_per_page;

$query = "SELECT * FROM tbl_student order by student_id DESC LIMIT $start_from, $record_per_page";
$result = mysqli_query($connect, $query);

?>

<!DOCTYPE html>
<html>
 <head>
  <title>Webslesson Tutorial | PHP Pagination with Next Previous First Last page Link</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <style>
  a {
   padding:8px 16px;
   border:1px solid #ccc;
   color:#333;
   font-weight:bold;
  }
  </style>
 </head>
 <body>
  <br /><br />
  <div class="container">
   <h3 align="center">PHP Pagination with Next Previous First Last page Link</h3><br />
   <div class="table-responsive">
    <table class="table table-bordered">
     <tr>
      <td>Name</td>
      <td>Phone</td>
     </tr>
     <?php
     while($row = mysqli_fetch_array($result))
     {
     ?>
     <tr>
      <td><?php echo $row["student_name"]; ?></td>
      <td><?php echo $row["student_phone"]; ?></td>
     </tr>
     <?php
     }
     ?>
    </table>
    <div align="center">
    <br />
    <?php
    $page_query = "SELECT * FROM tbl_student ORDER BY student_id DESC";
    $page_result = mysqli_query($connect, $page_query);
    $total_records = mysqli_num_rows($page_result);
    $total_pages = ceil($total_records/$record_per_page);
    $start_loop = $page;
    $difference = $total_pages - $page;
    if($difference <= 5)
    {
     $start_loop = $total_pages - 5;
    }
    $end_loop = $start_loop + 4;
    if($page > 1)
    {
     echo "<a href='pagination.php?page=1'>First</a>";
     echo "<a href='pagination.php?page=".($page - 1)."'><<</a>";
    }
    for($i=$start_loop; $i<=$end_loop; $i++)
    {     
     echo "<a href='pagination.php?page=".$i."'>".$i."</a>";
    }
    if($page <= $end_loop)
    {
     echo "<a href='pagination.php?page=".($page + 1)."'>>></a>";
     echo "<a href='pagination.php?page=".$total_pages."'>Last</a>";
    }
    
    
    ?>
    </div>
    <br /><br />
   </div>
  </div>
 </body>
</html>


Database



--
-- Database: `testing`
--

-- --------------------------------------------------------

--
-- Table structure for table `tbl_student`
--

CREATE TABLE IF NOT EXISTS `tbl_student` (
  `student_id` int(11) NOT NULL AUTO_INCREMENT,
  `student_name` varchar(250) NOT NULL,
  `student_phone` varchar(20) NOT NULL,
  PRIMARY KEY (`student_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;

--
-- Dumping data for table `tbl_student`
--

INSERT INTO `tbl_student` (`student_id`, `student_name`, `student_phone`) VALUES
(1, 'Pauline S. Rich', '412-735-0224'),
(2, 'Sarah C. White', '320-552-9961'),
(3, 'Samuel L. Leslie', '201-324-8264'),
(4, 'Norma R. Manly', '478-322-4715'),
(5, 'Kimberly R. Castro', '479-966-6788'),
(6, 'Elaine R. Davis', '701-685-8912'),
(7, 'Concepcion S. Gardner', '607-829-8758'),
(8, 'Patricia J. White', '803-789-0429'),
(9, 'Michael M. Bothwell', '214-585-0737'),
(10, 'Ronald C. Vansickle', '630-571-4107'),
(11, 'Clarence A. Rich', '904-459-3747'),
(12, 'Elizabeth W. Peterson', '404-380-9481'),
(13, 'Renee R. Hewitt', '323-350-4973'),
(14, 'John K. Love', '337-229-1983'),
(15, 'Teresa J. Rincon', '216-394-6894'),
(16, 'Erin S. Huckaby', '503-284-8652'),
(17, 'Brian A. Handley', '989-304-7122'),
(18, 'Michelle A. Polk', '540-232-0351'),
(19, 'Wanda M. Brown', '718-262-7466'),
(20, 'Phillip A. Hatcher', '407-492-5727'),
(21, 'Dennis J. Terrell', '903-863-5810'),
(22, 'Britney F. Johnson', '972-421-6933'),
(23, 'Rachelle J. Martin', '920-397-4224'),
(24, 'Leila E. Ledoux', '615-425-9930'),
(25, 'Darrell A. Fields', '708-887-1913'),
(26, 'Linda D. Carter', '909-386-7998'),
(27, 'Melva J. Palmisano', '630-643-8763'),
(28, 'Jessica V. Windham', '513-807-9224'),
(29, 'Karen T. Martin', '847-385-1621'),
(30, 'Jack K. McDonough', '561-641-4509'),
(31, 'John M. Williams', '508-269-9346'),
(32, 'Amelia W. Davis', '347-537-8052'),
(33, 'Gertrude W. Lawrence', '510-702-7415'),
(34, 'Michael L. Harris', '252-219-4076'),
(35, 'Casey A. Groves', '810-334-9674'),
(36, 'James H. Wilson', '865-259-6772'),
(37, 'James A. Wesley', '443-217-1859'),
(38, 'Armando C. Gay', '716-252-9230'),
(39, 'James M. Duarte', '402-840-0541'),
(40, 'Jason E. West', '360-610-7730'),
(41, 'Gloria H. Saucedo', '205-861-3306'),
(42, 'Paul T. Moody', '914-683-4994'),
(43, 'Sandra L. Williams', '310-335-1336'),
(44, 'Elaine T. Deville', '626-513-8306'),
(45, 'Robyn L. Spangler', '754-224-7023'),
(46, 'Sam A. Pino', '806-823-5344'),
(47, 'Joseph H. Marble', '201-917-2804'),
(48, 'Mark M. Bassett', '206-592-4665'),
(49, 'Edgar M. Billy', '978-365-0324'),
(50, 'Connie M. Yang', '815-288-5435');