Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Friday, February 13, 2009

Array Paging

Hi friends..below is the complete php code for array pagination. copy the code, paste it in a new file named array_paging.php and run it on your local server.

Comments are written to understand the code.

Please write any suggestions or improvements you have to implement in this code.


<?php
//This is the temporary array created for this example. In your case the values would be coming from database
$DataArray = array('value1','value2','value3','value4','value5','value6','value7','value8','value9','value10','value11','value12','value13','value14','value15','value16','value17');

//take the count of the array
$cnt = count($DataArray);

//get the page number from the url
if($_REQUEST['pge_no']){
$pge_no=$_REQUEST['pge_no'];
}else{
$pge_no=1; //set page number to 1 if not specified
}
if($_REQUEST['lim']){
$lim=$_REQUEST['lim'];
}else{
$lim=2; //Number of records to display per page.
}


if(!$cnt)
{
$cnt=0;
}
//calculate the number of pages
$pages = ceil($cnt/$lim);
if($pge_no>$pages)
{
$pge_no=$pages;
}
$pg_last=$pge_no*$lim; //calculate the last page value
$pg_start=$pg_last-($lim-1); //calculate the first page value


$counter=0;
//create the sets of pages eg. page number 1 will have 1,2,3 records, 2 will have 4,5,6 records...
for($l=1;$l<=$pages;$l++)
{

for($m=1;$m<=$lim;$m++)
{
$pagesets[$l][]=$counter;
$counter++;
}

}

//display the records
for($k=0;$k<count($DataArray);$k++)
{
//display the records only for the particular page
if(in_array($k,$pagesets[$pge_no]))
{
echo $DataArray[$k]."<br><hr/>";
}
}

//this is the number of pages to be displayed at a time between next and previous buttons
$NumberOfPagesToShow = 3;
if($pages>1){
$sets = ceil($pages/$NumberOfPagesToShow);

//createing first and last page numbers
for($j=1;$j<=$sets;$j++)
{
$lastpage = $j*$NumberOfPagesToShow;
$firstpage = $lastpage-$NumberOfPagesToShow+1;

$nextPageValue = $pge_no + 1; //calculate value of next button
if($nextPageValue>$pages)
{
$nextPageValue=$pages;
}
$next = "<a href='array_paging.php?pge_no=$nextPageValue'>Next »</a>"; //create next button

//create value of last button
$ending = $lastpage+1;
if($ending>$pages)
{
$ending = $pages;
}
$end = "<a href='array_paging.php?pge_no=$ending'>»</a>"; //Create last button

$prevPageValue = $pge_no - 1; //calculate value of previous button
if($prevPageValue<=0)
{
$prevPageValue=1;
}
$prev = "<a href='array_paging.php?pge_no=$prevPageValue'>« Previous</a>"; //create previous button

//create value of first button
$starting = $firstpage-1;
if($starting < 1)
{
$starting = 1;
}
$start = "<a href='array_paging.php?pge_no=$starting'>«</a>";//create first button

if($pge_no>=$firstpage && $pge_no<=$lastpage)
{
break;
}
}
echo $start." "; //display first button
echo $prev." "; //display previous button
//Display pages
for ($x=$firstpage;$x<=$lastpage;$x++){
if($x==$pge_no){
echo "<span style='color:red'>".$x."</span> ";
}else{
if($x<=$pages)
{
echo "<a href='array_paging.php?pge_no=$x'>".$x."</a> ";
}
}
}
echo $next." "; //display next button
echo $end." "; //display last button
echo "<br><br>";
}

?>