This is the simple example about how to do pagination in PHP.

/*
Function to get simple query converted into pagination query
*/

function getPagingQuery($sql, $itemPerPage = 10)
{
if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
$page = (int)$_GET['page'];
} else {
$page = 1;
}
// start fetching from this row number
$offset = ($page – 1) * $itemPerPage;
return $sql . "LIMIT $offset, $itemPerPage";
}

/*
Main function to do the pagination
Get the links to navigate between one result page to another.
Supply a value for $strGet if the page url already contain some
GET values for example if the original page url is like this :

http://www.abc.com/index.php?c=12

use "c=12" as the value for $strGet. But if the url is like this :

http://www.abc.com/index.php

then there's no need to set a value for $strGet
*/

function getPagingLink($sql, $itemPerPage = 10, $strGet = '')
{
$result = dbQuery($sql);
$pagingLink = '';
$totalResults = dbNumRows($result);
$totalPages = ceil($totalResults / $itemPerPage);

// how many link pages to show
$numLinks = 10;

// create the paging links only if we have more than one page of results
if ($totalPages > 1) {

$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;

if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
$pageNumber = (int)$_GET['page'];
} else {
$pageNumber = 1;
}

// print 'previous' link only if we're not
// on page one
if ($pageNumber > 1) {
$page = $pageNumber – 1;
if ($page > 1) {
$prev = " <a href=\"$self?page=$page&$strGet/\">[Prev]</a> ";
} else {
$prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
}

$first = " <a href=\"$self?$strGet\">[First]</a> ";
} else {
$prev = ''; // we're on page one, don't show 'previous' link
$first = ''; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($pageNumber < $totalPages) {
$page = $pageNumber + 1;
$next = " <a href=\"$self?page=$page&$strGet\">[Next]</a> ";
$last = " <a href=\"$self?page=$totalPages&$strGet\">[Last]</a> ";
} else {
$next = ''; // we're on the last page, don't show 'next' link
$last = ''; // nor 'last page' link
}

$start = $pageNumber – ($pageNumber % $numLinks) + 1;
$end = $start + $numLinks – 1;

$end = min($totalPages, $end);

$pagingLink = array();
for($page = $start; $page <= $end; $page++) {
if ($page == $pageNumber) {
$pagingLink[] = " $page "; // no need to create a link to current page
} else {
if ($page == 1) {
$pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
} else {
$pagingLink[] = " <a href=\"$self?page=$page&$strGet\">$page</a> ";
}
}
}
$pagingLink = implode(' | ', $pagingLink);

// return the page navigation link
$pagingLink = $first . $prev . $pagingLink . $next . $last;
}
return $pagingLink;
}

/************************************************

This will come in a page where you want to do the pagination

$queryString = '';
// for paging
// how many rows to show per page
$rowsPerPage = 10;

$sql = "SELECT * FROM tbl_members ORDER BY name";
$result = mysql_query(getPagingQuery($sql, $rowsPerPage));
$pagingLink = getPagingLink($sql, $rowsPerPage);

/*********************************************************

Where you want to display the pagination write the code below

<?php echo $pagingLink; ?>

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Reddit Post to StumbleUpon

Share or Bookmark This Post With :

13
Dec

Here i’m going to write, how to do pagination in Java. The situation is first count how many rows in the database table, then query the database by page size (how many rows you want to display on a web page).

In Jsp

String pno=request.getParameter("pno"); // this will be coming from url
int pageno=0;
if(pno==null)
{
pageno=1;
}
else
{
pageno=Integer.parseInt(pno);
}
int next=pageno+1;
int previous=pageno-1;

//Java method
ArrayList al=object.getData(pageno);

// do all your showing data logic here

//print the previous and next links
if(previous!=0){
Previous link (url.jsp?pno=previous)
}

int max=obj.getTotalRows(); // total number of rows from database table

int check=(next-1)*10; // each page 10 rows

if(check<max){ // print next link if check is less than total rows
Next link (url.jsp?pno=next)
}

In Java

//for getting the data according to page number
public ArrayList getData(int pagenumber)
{
ArrayList al=new ArrayList();
int offset = (pagenumber – 1) * 10;

//write all the logic and query data with the limit offset

Query + limit offset ,10;

return al;
}

//for getting total rows

public int getTotalRows() {
//write your logic to get total count of the rows
Query total rows from db table

return total_rows;
}

Post to Twitter Post to Yahoo Buzz Post to Delicious Post to Digg Post to Reddit Post to StumbleUpon

Share or Bookmark This Post With :

   
Subscribe to RSS Feeds Follow me on Twitter Add to Favourite
 
Sponsors
 
Tags
 
Network [+]
 
© Copyright 2009 - 2011 TechiePark.com. All Rights Reserved | Powered by WordPress
This work is licensed under a Creative Commons Attribution 3.0 Unported License
Php5 powered    Mysql powered    Apache powered   Best viewed with  Spread Firefox Affiliate Button