[Solved] How to add pagination in php


You need to first count the number of rows in your present query:

$numrows = $s->rowCount();

and need to place a vaiable for results per page say $resultsPerPage:

$resultsPerPage=10;

Then the page you are currenty in:

$offset=$_REQUEST['offset'];

Then you need to run the below code :

    $limit=$resultsPerPage;
    $PHP_SELF=$_SERVER['PHP_SELF'];
    if($numrows >= 1) { 
           // determine if offset has been passed to script, or if offset has been tampered with.
            if (empty($offset) || ($offset < 0) || ($offset > $numrows)) {
                $offset=0;
            }
            // Determine if a "PREV" link is necessary - if so, add it to the links array
            if (($offset > 0) && ($offset <= $numrows)) { 
                $prevoffset = $offset - $limit;
                $link_array[] = "<a href=\"$PHP_SELF?offset=$prevoffset" . $addOn . "\">Prev</a> &nbsp; \n";
            }

            // Determine the total number of pages needing links
            $pages=intval($numrows/$limit);
            // $pages variable now contains integer number of pages needed, unless there is a remainder from division
            if ($numrows % $limit) {
                // There is a remainder, so add one page
                $pages++;
            }
        /*
            for ($i=1; $i<=$pages; $i++) { // loop thru
                $newoffset=$limit*($i-1);
                if ((intval($offset/$limit)) == (intval($i-1))) 
                {   $link_array[] = "[$i] &nbsp; \n"; }
                else {  
                    $link_array[] = "<a href=\"$PHP_SELF?offset=$newoffset" . $addOn . "\">$i</a> &nbsp; \n"; 
                }
            }
            */

        $start_page=intval($offset/$limit)-4;
        $end_page=intval($offset/$limit)+5;

        if($start_page<=0){
        $start_page=1;  

        }

        if($start_page<2){
        $end_page=10;  

        }

        if($end_page>$pages){
          $end_page=$pages;
        }



            for ($i=$start_page; $i<=$end_page; $i++) { // loop thru
                $newoffset=$limit*($i-1);

            if ((intval($offset/$limit)) == (intval($i-1))) 
                {   $link_array[] = "[$i] &nbsp; \n"; }
                else {  
                $link_array[] = "<a href=\"$PHP_SELF?offset=$newoffset" . $addOn . "\">$i</a> &nbsp; \n"; 
                }
            }

            // Determine if this is the last page.
            if (!(($offset/$limit)==$pages) && $pages!=1) {
                $newoffset=$offset+$limit;
                // if not last page give NEXT link
                if((($numrows - $offset) > $limit) && ($pages !=1) && ($offset < $numrows)){
                    $link_array[] = "<a href=\"$PHP_SELF?offset=$newoffset" . $addOn . "\">Next</a><br>\n";
                }
            }
        }else{
            ; // redirect to error page
        }

if ($resultsPerPage > 0  && count($link_array) > 1)
{   echo "Page: ";
    array_walk($link_array, 'printArray'); 
}

3

solved How to add pagination in php