1

i am trying to create a pagination in which there are 5 items at a time depending on the number of items in DB. i wrote this code . but i dono how to go further .its buggy..any better pagination or alteration for this

<?php

$myresult .= "<div class='pagination' >";

if ($pagenum == 1)
{
}
else
{
$pagenum = 1;
$myresult .= "<a href='javascript:newPage(\"".$pagenum."\")'>&nbsp;first&nbsp;</a>";

$myresult .= " ";

$previous = $pagenum-1;
$myresult .= "<a href='javascript:newPage(\"".$previous."\")'> &nbsp;Prev&nbsp;</a>";

if ($pagenum == $last)
{

$previous3 = $pagenum-4;
$myresult .= "<a href='javascript:newPage(\"".$previous3."\")'> &nbsp; $previous3 &nbsp;</a>";

$previous2 = $pagenum-3;
$myresult .= "<a href='javascript:newPage(\"".$previous2."\")'> &nbsp; $previous2 &nbsp;</a>";
}
if ($pagenum > 2)
{
$previous1 = $pagenum-2;
$myresult .= "<a href='javascript:newPage(\"".$previous1."\")'> &nbsp; $previous1 &nbsp;</a>";

}
if ($pagenum > 1)
{

$previous2 = $pagenum-1;
$myresult .= "<a href='javascript:newPage(\"".$previous2."\")'> &nbsp; $previous2 &nbsp;</a>";
$myresult .= " ";

}



}

$myresult .= "<span class=\"disabled\"> $pagenum </span>";



if ($pagenum == $last)
{
}
else {

if($pagenum < $last - 1)
{
$next = $pagenum+1;
$myresult .= "<a href='javascript:newPage(\"".$next."\")'>&nbsp; $next &nbsp;</a>";
}
if($pagenum < $last - 2)
{
$next1 = $pagenum+2;
$myresult .= "<a href='javascript:newPage(\"".$next1."\")'> &nbsp; $next1 &nbsp;</a>";
}
if($pagenum == 1 )
{
$next2 = $pagenum+3;
$myresult .= "<a href='javascript:newPage(\"".$next2."\")'> &nbsp; $next2 &nbsp;</a>";

$next3 = $pagenum+4;
$myresult .= "<a href='javascript:newPage(\"".$next3."\")'> &nbsp; $next3 &nbsp;</a>";
}
if($pagenum == 2 )
{
$next2 = $pagenum+3;
$myresult .= "<a href='javascript:newPage(\"".$next2."\")'> &nbsp; $next2 &nbsp;</a>";
}
$next = $pagenum+1;
$myresult .= "<a href='javascript:newPage(\"".$next."\")'>&nbsp;Next&nbsp;</a>";


$myresult .= "<a href='javascript:newPage(\"".$last."\")'>&nbsp;Last</a>";

}
$myresult .= "</div>";
$myresult .= "</br>";
?>
3
  • Pagination depends much on the graphic aspect that you intend to achieve! Do you have a picture/screenshot/whatever, of what you're trying to create ? With that, I can give you a hand... Regarding your present code, you're repeating the same statements allot, and as you said, it's very buggy :) Commented Jun 2, 2010 at 21:13
  • drupal.org/node?page=2 i am trying to create something like the pagination at bottom. say pagination buttons like first,prev,3,4,5,6,7,next,last . getting first,prev,next,last was easy but the center 5 page links is difficult for me Commented Jun 3, 2010 at 5:06
  • i found the solution . its at phpeasystep.com/phptu/29.html Commented Jun 3, 2010 at 6:29

1 Answer 1

2

Maybe not the best answer ever but here is something I have used:

<?php

class Pagination {

    function __construct() {

    }

    function getPaginatinationNavigation($page = 1, $num_rows, $rows_per_page, $page_name)
    {
        $lastpage = ceil($num_rows/$rows_per_page);

        $page = (int)$page;
        if ($page > $lastpage) 
        {
            $page = $lastpage;
        } 
        if ($page < 1) {
            $page = 1;
        } 

        $content='<p style="text-align: center;">';

        if ($page == 1) {
            $content.= " FIRST PREV ";
        } else {
            $content.= " <a href='$page_name?page=1'>FIRST</a> ";
            $prevpage = $page-1;
            $content.= " <a href='$page_name?page=$prevpage'>PREV</a> ";
        } 

        $content.= " ( Page $page of $lastpage ) ";

        if ($page == $lastpage) {
            $content.= " NEXT LAST ";
        } else {
            $nextpage = $page+1;
            $content.= " <a href='$page_name?page=$nextpage'>NEXT</a> ";
            $content.= " <a href='$page_name?page=$lastpage'>LAST</a> ";
        } 

        $content.= '</p>';

        return $content;

    }
}

?>

Import the Object

require_once('classes/Pagination.php');

Get the amount of total rows:

$query= "SELECT COUNT(*) FROM TABLE_NAME";
$row = mysql_fetch_array($getResults);
$numRecords = $row[0];

Make the LIMIT to limit the rows based upon page number:

$limit = ' LIMIT ' . ($page - 1) * 25 .', 25';

Query USING LIMIT

$query = 'SELECT * FROM TABLE_NAME' . $limit;
$getResults=mysql_query($query) or die(mysql_error());

Display the Results as you normally would:

while($row = mysql_fetch_array($getResults))
{
 DISPLAY RESULTS HERE
}

Use the Class to spit out Navigation:

$pagination->getPaginatinationNavigation($page, $numRecords, 25, 'display_page_name.php');

So Overall, for pagination you need to make 2 queries:

1) To get the total amount of records in your search 2) To get the Records LIMITed to those items in a range: say 25 to 50 or 1000 to 1050

You display records from the limited query and to go to the next set of records you increase the page number by one.

Hope this helps. Don't forget to scrub any data you get from the url querystring.

Let me know if you would like me to explain further.

Sign up to request clarification or add additional context in comments.

3 Comments

the back end code of fetching of data is being done . drupal.org/node?page=2 i am trying to create something like the pagination at bottom. say pagination buttons like first,prev,3,4,5,6,7,next,last
question, once you call the function once, you get the total number of pages and total number of results. On subsequent calls to the function when you're going to the following pages, is there a way to not make the call to get the total number of results? You're basically making two trips to the DB when loading a "new page."
luckytaxi, it seems like this is the best way to go about doing pagination that I know of. So yes 1 connection, 2 queries, one for the total amount of records and then one to limit the records returned to the current page's set of records, which is the one you display. I guess you could store those two pieces of data into a session variable(s) and use those to make your pagination. If you are using a table with many transactions though you may end up with an incomplete or invalid paginated result set. In my experience using two queries is best case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.