25 January 2011

Php Array Pagination Script

Sometimes we are trapped in a situation where we have thousands of elements in an array variable and we can not just show this much data on the page. So for such situations I am sharing a very easy simple to integrate php array pagination script. By using this script one can easily paginate an array variable with thousands of elements.

Complete paging script is mentioned below:
<?php
///////////////FILLING ARRAY WITH DUMMY DATA////////////////////
$key = array();
for($i=0; $i<200; $i++)
{
 //fill array data
 $key[] = "num = ".$i;
}
////////////////////////////////////////////////////////////////

/////////////////////START OF ARRAY PAGINATION CODE/////////////////////
$ptemp="http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$pt=explode('&',$ptemp);
if (strpos($ptemp,'pageno'))
 array_pop($pt);
$pt=implode('&',$pt);
$ptemp=$pt;
$array=$key; // REPLACE $KEY WITH YOUR ARRAY VARIABLE
$page = $_REQUEST['pageno'];

$currentpage = isset($page) ? (integer)$page : 1;
$numperpage = 10; //NUMBER OF RECORDS TO BE DISPLAYED PER PAGE

$total = count($array);
$numofpages = ceil($total / $numperpage); //TOTAL NUMBER OF PAGES

if(isset($array))
{
    if (($currentpage > 0) && ($currentpages <= $numofpages))
 {
        //STARTING LOOP FOR ARRAY DATA
        $start = ($currentpage-1) * $numperpage;
        for($i=$start;$i<=($numperpage+$start-1);$i++) 
  {
            ///////////PLACE YOUR CODE HERE//////////////////////////
            echo $array[$i] .'<br />';
   ////////////////////////////////////////////////////////
        }
 }
}
if ($currentpage != 1) 
{ //GOING BACK FROM PAGE 1 SHOULD NOT BET ALLOWED
 $previous_page = $currentpage - 1;
 $previous = '<a href="'.$ptemp.'?pageno='.$previous_page.'"> <</a> ';    
}    
$pages = '';
for ($a=1; $a<=$numofpages; $a++)
{
  if ($a == $currentpage) 
 $pages .= $a .'</u> ';
  else 
 $pages .= '<a href="'.$ptemp.'?pageno='.$a.'" >'. $a .'</a> ';
}
$pages = substr($pages,0,-1); //REMOVING THE LAST COMMA (,)

if ($currentpage != $numofpages) 
{ //GOING AHEAD OF LAST PAGE SHOULD NOT BE ALLOWED
 $next_page = $currentpage + 1;
 $next = ' <a href="'.$ptemp.'?pageno='.$next_page.'"> ></a>';
}
echo '<br /><br />'. $previous . $pages . $next; //PAGINATION LINKS
/////////////////////END OF ARRAY PAGINATION CODE/////////////////////
?>
For any queries or problems that you may have while integrating this script, feel free to ask by commenting on this post.

Related Post: Mysql Php Pagination Script

7 comments:

Anonymous said...

Thanks for this script! I am having a small problem with it. I have four items in my array arranged in a certain order (1,2,3,4). When I implement your script (separating into 2 pages of 2 items), it is reversing the order (4,3|2,1). Any suggestions?

Muhammad Bilal said...

Without Checking your code, I can not tell you anything, But if you can mail me the code then I will look into it.

Anil said...

hi friend i am New in php. please can you combine this paging in directory listing v3. Dirlistingv3.

if you can do this. please help. i will very thanksful to you.

please contact me - varey.dj@gmail.com

Thanks For Your Future Help!!

Anonymous said...

Thank so much for your script its awsome

M Amir Hamza said...

I am unable to post php code. Anyways i hope you will understand my problem. Please modify your script with 25 records to display with 10 records per page. Also display numbering you will see on last page it shows 25 records but it will keep continue with numbering upto 30 which should not be 30.

Anonymous said...

What would be the best way to display the pagination code on the top and bottom of the pages if I have thousands of results and I only want let's say ten at a time to appear?

Anonymous said...

Thank you for sharing.

I have but one question. Is there a way I can incorporate this php script so that it appears only at the top and bottom of a search results page?