I have few tables in my database and want to implement pagination for several tables on the pages.
I wrote my own code for pagination in each page wherever it is required.
Is there any frame work which can be easily implemented like, when I pass a query to a method or class it has to generate ui with proper pagination.Would any body suggest me any frame work which does this task.
edit:
I already specified that I wrote my own code for pagination, it is something very close to the one solution given by @veerendra but for pagination in 60 different pages I need to write that logic.
I asked for a framework which reduces my burden of writing the code for pagination which is dynamic and it should be in php not jquery or javascript.
As thier logic will reside at client side can't operate on large sets of data.
Below is the file named paging.inc.php
You just need to pass the where class to the query and just need to get the count only and name the count varibale same as in the file then you will need to set the records per page and other parameters.
After this you need to fire the query which will bring the records with the limit variable from the file and this can be used for sorting the columns in the listing.
<?
//CODE FOR PAGING
if(!isset($num_totrec)) $num_totrec = $db_recs[0]["tot"];
//$num_totrec SHOULD BE PASSED
if(!isset($pg_limit) && empty($pg_limit))
$pg_limit = $PAGE_LIMIT; //page limit
if(!isset($rec_limit) && empty($rec_limit))
$rec_limit = $REC_LIMIT; //record limit
if($_REQUEST["TotalRecords"]!="")
$rec_limit = $_REQUEST["TotalRecords"];
$num_tmp = 0;
$var_flg = "0";
$var_limit = "";
$num_limit = 0;
$var_filter = "";
if($_GET[start]=='') {$_GET[start] = 1;$start=1;}
if(($_GET[start]-1)*$rec_limit > $num_totrec)
{
$_GET[start]=1;
$start=1;
}
if(isset($tempvar) && !empty($tempvar))
{
if($stat==1 && $tempvar == "true") $stat=0;
else if($stat==0 && $tempvar == "true") $stat=1;
else $stat=1;
}
$sort_order = ($stat==1)? $asc_order:$desc_order;
$sort_img = "<img src='$sort_order' border='0'>";
$var_filter = "";
$var_filter = "&stat=$stat";
//CODE FOR COLUMN SHOULD SORT ASCENDING/DESCENDING - END
//CHANGE THIS CODE WITH SUITABLE VARIABLES
if(isset($ptype)) $var_filter.= "&ptype=".$ptype."&action=Section";
foreach ($_GET as $key=>$val)
{
if($key != "stat" && $key != "start" && $key != "nstart" && $key != "tempvar" && $key != "sorton")
{
if(is_array($val))
{
for($k=0;$k<count($val);$k++)
{
$var_filter .= '&'.$key.'[]=' . $val[$k];
}
}else if($key == "keyword"){
$var_filter.= "&$key=". stripcslashes($val);
}
else
$var_filter.= "&$key=$val";
}
}
foreach ($_POST as $key=>$val)
{
if($key != "stat" && $key != "start" && $key != "nstart" && $key != "tempvar" && $key != "sorton")
{
if(is_array($val))
{
for($k=0;$k<count($val);$k++)
{
$var_filter .= '&'.$key.'[]=' . $val[$k];
}
}else if($key == "keyword"){
$var_filter.= "&$key=". stripcslashes($val);
}
else
$var_filter.= "&$key=$val";
}
}// end
if(isset($month_val) && $month_val != "") $var_filter.= "&month_val=$month_val";
if(isset($year_val) && $year_val != "") $var_filter.= "&year_val=$year_val";
if(isset($action) && $action != "") $var_filter.= "&action=$action";
//SET Extra querystring variables to pass from here
//$var_extra can be attached with the links for this purpose
if(isset($start)){
$num_limit = ($start-1)*$rec_limit;
$var_limit = " LIMIT $num_limit,$rec_limit";
}else $var_limit = " LIMIT 0,$rec_limit";
if(!isset($nstart)){
if($num_totrec){ //if recs exists!!
if($rec_limit>$num_totrec){
$num_pgs = 1;
$var_flg = "2";
}else{
$num_loopctr =0;
$num_loopctr = ceil($num_totrec/$rec_limit);
if($pg_limit>$num_loopctr){
$num_pgs = $num_loopctr;
$var_flg = "2";
}else{
$num_pgs = $pg_limit;
if($num_totrec<=($rec_limit*$pg_limit)) $var_flg = "2";
else $var_flg = "1";
}
}
$var_link = "";
$var_prevlink ="";
//if sorting is set
$var_sort_link="";
if(isset($sorton)) $var_sort_link = "&sorton=$sorton";
$var_prevlink ="<font size=1 color=black> |";
for($i=1;$i<=$num_pgs;$i++)
{
IF($i==1)
$var_link.= "<font size=1 color=red>$i</font> | ";
else
$var_link.= "<a href=\"$var_self$PHP_SELF?nstart=1&start=$i$var_filter$var_sort_link$var_extra\"><font size=1 color=black>$i</font></a> | ";
}
if($var_flag !="0" and $var_flg!="2"){ $var_link .= " > <a href=\"$var_self$PHP_SELF?nstart=2&start=$i$var_filter$var_filter$var_sort_link$var_extra\"><font size=1 color=black>next</font></a>"; }else {$var_link .= " </font>";
}
$page_link = "";
$page_link = "$var_prevlink $var_link";
}else{
//IF NO RECORDS EXISTS!!
$var_link="";
}
}else{ //if nstart is set
if($num_totrec){ //if recs exists!!
$num_loopctr =0;
$num_rem_rec = 0;
$num_rem_rec = ($num_totrec-(($nstart-1)*$rec_limit*$pg_limit));
$num_loopctr = ceil($num_rem_rec/$rec_limit);
$num_tmp = $rec_limit*$nstart*$pg_limit;
if($num_tmp>$num_totrec){
$num_pgs = $num_loopctr;
$var_flg = "2";
}else{
$num_pgs = $pg_limit;
if($num_totrec==($nstart*$rec_limit*$pg_limit)) $var_flg = "2";
else $var_flg = "1";
}
$var_link = "";
$var_prevlink ="";
//if sorting is set
$var_sort_link="";
if(isset($sorton)) $var_sort_link = "&sorton=$sorton";
$num_prevnstart = 0;
$num_prevstart = 0;
$num_prevnstart = $nstart-1;
$num_prevstart = ($nstart*$pg_limit)-$pg_limit;
$num_tmp = ($num_totrec/$rec_limit);
if($nstart<=1) $var_prevlink ="<font size=1 color=black> |";
else $var_prevlink ="<a href=\"$var_self$PHP_SELF?nstart=$num_prevnstart&start=$num_prevstart$var_filter$var_sort_link$var_extra\"><font size=1 color=black>prev</font></a> <font size=1 color=black>< |</font>";
for($i=1;$i<=$num_pgs;$i++)
{
$num_start = $num_prevstart+$i;
$num_nstart = $nstart+1;
IF($start==$num_start)
$var_link.= "<font size=1 color=red>$num_start</font> | ";
else
$var_link.= "<a href=\"$var_self$PHP_SELF?nstart=$nstart&start=$num_start$var_filter$var_sort_link$var_extra\"><font size=1 color=black>$num_start</font></a> | ";
}
$num_start++;
if($var_flag!="0" and $var_flg!="2"){ $var_link .= " ><a href=\"$var_self$PHP_SELF?nstart=$num_nstart&start=$num_start$var_filter$var_sort_link$var_extra\"><font size=1 color=black>next</font></a></font>"; }else {$var_link .= "<font size=1 color=black> </font>";
}
$page_link = "";
$page_link = "$var_prevlink $var_link";
}else{
//IF NO RECORDS EXISTS!!
$var_link="";
}
}
//if set the paging variables
if(isset($nstart)) $var_pgs = "&nstart=$nstart&start=$start"; //attach this with the sorting links
//CODE FOR PAGING ENDS OVER HERE
?>
Posting a sample code below so you can get what i said in the above description.
This is just a sample how i use it.
$where_arr = array();
if($keyword !=""){
$where_arr[] ="$option like '".$keyword."%'";
}
if(count($where_arr)>0)
$where_clause = " WHERE ".implode(" AND ", $where_arr);
else
$where_clause = '';
$table_clause = " from admin";
$sql = "select count(iAdminId) as tot ".$table_clause.$where_clause;
$rs_sql = $sqlObj->select($sql);
$num_totrec = $rs_sql[0]['tot'];
include("gen_pagingmsg.inc.php");
if($sorton != "")
{
switch ($sorton)
{
case "1":
$sort = "vUserName";
if($stat!=1) $sort .= " DESC";
break;
case "2":
($stat==1)? $sort = "vFirstName, vLastName" : $sort = "vFirstName desc, vLastName DESC";
break;
case "3":
$sort = "vEmail";
if($stat!=1) $sort .= " DESC";
break;
case "4":
$sort = "dLastAccess";
if($stat!=1) $sort .= " DESC";
break;
case "5":
$sort = "iTotLogin";
if($stat!=1) $sort .= " DESC";
break;
case "6":
$sort = "eStatus";
if($stat!=1) $sort .= " DESC";
break;
default:
$sort ="vUserName";
}
}else {
$sort ="vUserName";
}
$sql = "select iAdminId, concat(vFirstName, ' ', vLastName) as vName, vUserName, vEmail, dLastAccess, iTotLogin, eStatus ".$table_clause.$where_clause." order by ".$sort.$var_limit;
//echo "<br>".$sql;
$db_sql = $sqlObj->select($sql);
Below is the gen_pagingmsg.inc.php
<?
# =========================================================================
# Paging Paging comes from this File. Don't Remove this below line.
# =========================================================================
//echo $num_totrec;
//ECHO $rec_limit ;
if($ADMIN_SHOWPAGING_TOP=="N" && $ADMIN_SHOWPAGING_BOTTOM=="N")
$rec_limit=$num_totrec;
include("paging.inc.php");
# =========================================================================
//$keyword = stripcslashes($keyword);
if($keyword!="")
{
$var_msg="Your search for #keyword# has found #num_totrec# matches:";
$var_msg=str_replace("#keyword#","<font color=#000000>$keyword</font>",$var_msg);
$var_msg=str_replace("#num_totrec#","<font color=#000000>$num_totrec</font>",$var_msg);
}
if(!isset($start))
$start = 1;
$num_limit = ($start-1)*$rec_limit;
$startrec = $num_limit;
$lastrec = $startrec + $rec_limit;
$startrec = $startrec + 1;
if($lastrec > $num_totrec)
$lastrec = $num_totrec;
if($num_totrec > 0 )
{
$recmsg = "Showing ".$startrec." - ".$lastrec." Records Of ".$num_totrec;
}
else
{
$recmsg="Sorry !... No Records Found";
}
?>
This file basically provide you the message and paging html which you can keep just above or below your listing
You can use the jquery pagination its simple and fast. Please follow the below steps i have described in depth
Get the zip containing the plugin and the CSS themes here.
Step 1: together with JQuery, include jquery.simplePagination.js in your page:
<script type="text/javascript" src="path_to/jquery.js"></script>
<script type="text/javascript" src="path_to/jquery.simplePagination.js"></script>
JQuery 1.7.2 or later is recommended. Older versions might work as well, but i have not tested.
Step 2: OPTIONAL - include the CSS file with the 3 default themes
<link type="text/css" rel="stylesheet" href="path_to/simplePagination.css"/>
If you skip this step, you will need to define your own styles or use Bootstrap.
Step 3: call the function on your pagination placeholder:
$(function() {
$(selector).pagination({
items: 100,
itemsOnPage: 10,
cssStyle: 'light-theme'
});
});
If necessary, you can specify the number of pages directly, via "pages" parameter, instead of "items" and "itemsOnPage" which are used by the plugin to automatically calculate the number of pages.
Available options
items Integer Default: 1 Total number of items that will be used to calculate the pages.
itemsOnPage Integer Default: 1 Number of items displayed on each page.
pages Integer Optional If specified, items and itemsOnPage will not be used to calculate the number of pages.
displayedPages Integer Default: 5 How many page numbers should be visible while navigating.
Minimum allowed: 3 (previous, current & next)
edges Integer Default: 2 How many page numbers are visible at the beginning/ending of the pagination.
currentPage Integer Default: 1 Which page will be selected immediately after init.
hrefTextPrefix String Default: "#page-" A string used to build the href attribute, added before the page number.
hrefTextSuffix String Default: empty string Another string used to build the href attribute, added after the page number.
prevText String Default: "Prev" Text to be display on the previous button.
nextText String Default: "Next" Text to be display on the next button.
labelMap Array Default: empty array A collection of labels that will be used to render the pagination items, replacing the numbers.
cssStyle String Default: "light-theme" The class of the CSS theme.
selectOnClick Boolean Default: true Set to false if you don't want to select the page immediately after click.
onPageClick(pageNumber, event) Function Optional Function to call when a page is clicked.
Page number and event are optional parameters.
onInit Function Optional Function to call when the pagination is initialized.
Available methods
selectPage - Select a page based on page number.
$(function() {
$(selector).pagination('selectPage', pageNumber);
});
prevPage - Selects the previous page.
$(function() {
$(selector).pagination('prevPage');
});
nextPage - Select the next page.
$(function() {
$(selector).pagination('nextPage');
});
getPagesCount - Returns the total number of pages.
$(function() {
$(selector).pagination('getPagesCount');
});
getCurrentPage - Returns the current page number.
$(function() {
$(selector).pagination('getCurrentPage');
});
disable - Disables pagination functionality.
$(function() {
$(selector).pagination('disable');
});
enable - Enables the pagination after it was previously disabled.
$(function() {
$(selector).pagination('enable');
});
destroy - Visually destroys the pagination, any existing settings are kept.
$(function() {
$(selector).pagination('destroy');
});
redraw - The pagination is drawn again using the existing settings. (useful after you have destroyed a pagination for example)
$(function() {
$(selector).pagination('redraw');
});
updateItems - allows to dynamically change how many items are rendered by the pagination
$(function() {
$(selector).pagination('updateItems', 100);
});
updateItemsOnPage - allows to dynamically change how many items are rendered on each page
$(function() {
$(selector).pagination('updateItemsOnPage', 20);
});
drawPage - takes a page number as a parameter and it sets the "currentPage" value to the given page number and draws the pagination
$(function() {
$(selector).pagination('drawPage', 5);
});