PHP Pagination Function

This is a Function For PHP Pagination With 5 Parameters:

$table=”labr_salary”;  // this is a name of mysql table
$rows=20;                    // the no. of rows you want in a page (it should be even).
$pages=5;                       // the no. of page numbers u want to display.
$url_name=”labour_accounts”; // this is current php page location on which you want rows to get printed.
$get_src=”set=0″;  // this is a parameter you need to pass if the current page is active because of some $_GET value passed just like in this picture. The if it is $_GET[‘set’]==0 then it gets executed.

php_pagination

If it doesnt require u can leave it cuz it has a default value in the functions parameter.
$account_set=set_pagination($table,$rows,$pages,$url_name,$get_src);

<?php   /////// Set Pagination Function with 5 Parameters ///////

function set_pagination($table,$rows,$page_no,$page_url,$get_m=””)
{
global $connection;
$table_name=$table;
$rows=$rows;
$pages=$page_no;
$url=$page_url;
$get=$get_m;
if($rows%2==0)
{
$count=0;
$query=”select * from {$table}”;
$page_set=mysql_query($query,$connection);
confirm_query($page_set);
while($page=mysql_fetch_array($page_set))
{
$count=$count+1;
}
$page_start=ceil($count/$rows);
echo “<br>”;
echo “<b>Page:</b>&nbsp;”;
// previous  page
if(isset($_GET[‘start’]))
{
if($_GET[‘start’]!=1)
{
$start=$_GET[‘start’];
$start_min=$start-1;
$left=”<<“;
echo “<a href=\”{$url}.php?{$get}&start={$start_min}\”>{$left}</a>”;
}
}
// default page number
if(!isset($_GET[‘start’]))
{
$start_min=$page_start-1;
$left=”<<“;
if($page_start>1)
echo “<a href=\”{$url}.php?{$get}&start={$start_min}\”>{$left}</a>”;
$start_page=0;
if($page_start<$pages)
{
$end_page=$page_start;
$start_page=1;
}
else
{
$end_page=$page_start;
$start_page=$page_start-($pages-1);
}
for($start=$start_page;$start<=$end_page;$start++)
{
if(!isset($_GET[‘start’])||$_GET[‘start’]!=$start)
echo “<a href=\”{$url}.php?{$get}&start={$start}\”>{$start}</a>&nbsp;”;
}
}
// minimum page displayed
if(isset($_GET[‘start’]))
{
$start_page=$_GET[‘start’];
if($page_start<$pages)
{
$end_page=$page_start;
}
else
{
$end_page=$start_page+($pages-1);
}
for($start=$start_page;$start<=$end_page;$start++)
{
if($start_page!=$start)
echo “<a href=\”{$url}.php?{$get}&start={$start}\”>{$start}</a>&nbsp;”;
else
echo $start.”&nbsp;”;
if($start==$page_start)
break;
}
}
// next page
if(isset($_GET[‘start’]))
{
if($_GET[‘start’]!=$page_start)
{
$start=$_GET[‘start’];
$start_ad=$start+1;
$right=”>>”;
echo “<a href=\”{$url}.php?{$get}&start={$start_ad}\”>{$right}</a>”;
}
}
$start=$count-($count%$rows);

if(isset($_GET[‘start’]))
{
$start=$_GET[‘start’];
if($start==1)
{
$start=0;
}
else
{
$start=$start*($rows/2)+($start-2)*($rows/2);
}
}
$query=”select * from {$table} limit {$start},{$rows}”;
$page_set=mysql_query($query,$connection);
confirm_query($page_set);
return $page_set;
}
/////// Condition if the Number is ODD
else
echo “Function Cannot Be Applied For Odd Numbers”;
}
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s