PaginationSimple Pagination

We have handled pagination concept with five project details display per page

 

Sample Code:

<?php define('MAX_REC_PER_PAGE', 5); define('MAX_LINKS_PER_PAGE', 10); // calculate total records $rs = mysql_query("SELECT COUNT(*) FROM bookmarklet_details where project_status='APPROVED'") or die("Count query error!"); list($total) = mysql_fetch_row($rs); $total_pages = ceil($total / MAX_REC_PER_PAGE); // calculate our starting point and range... $page = intval(@$_GET["page"]); // intval () forces to a numeric value to protect against garbage or malicious entries; intval ("string") is ZER0 // @ in front of a variable suppresses warnings if it isnt there, an empy string intval() will be ZERO if (0 == $page) $page = 1; // 1-based $start = MAX_REC_PER_PAGE * ($page - 1);//first record we will display $max = MAX_REC_PER_PAGE; // maximum number to show $newerpage=$page-1; $olderpage=$page+1; if ($newerpage>0) { echo "<a href=\"".$_SERVER["PHP_SELF"]."?page=$newerpage\">&larr; Newer</a>"; } if ($page> MAX_LINKS_PER_PAGE) { // first page echo "<a href=\"" . $_SERVER["PHP_SELF"] . "?page=1\">1</a> "; // second page echo "<a href=\"" . $_SERVER["PHP_SELF"] . "?page=2\">2</a> <span style=\"color:#777;\">...</span> "; } $currentstartpage=(intval(($page-1)/MAX_LINKS_PER_PAGE)*MAX_LINKS_PER_PAGE)+1; if (($total_pages>MAX_LINKS_PER_PAGE)) { $currentendpage=$currentstartpage+MAX_LINKS_PER_PAGE-1; if ($currentendpage>=$total_pages) { $currentendpage=$total_pages-2; } } else { $currentendpage=$total_pages-2; } for ($i = $currentstartpage; $i <= $currentendpage; $i++) { $currentpage = $i; if ($page != $i) # dont link to current page $currentpage="<a href=\"".$_SERVER["PHP_SELF"] ."?page=$i\">$currentpage</a>"; ?> <?php echo $currentpage; ?> <?php } if ($currentendpage!=$total_pages-2) { echo " <span style=\"color:#777;\">...</span> "; } // before last page $beforelast=$total_pages-1; if ($page==$beforelast) { echo $beforelast; } else { echo " <a href=\"" . $_SERVER["PHP_SELF"] . "?page=$beforelast\">$beforelast</a> "; } // last page if ($page==$total_pages) { echo $total_pages; } else { echo " <a href=\"" . $_SERVER["PHP_SELF"] . "?page=$total_pages\">$total_pages</a>"; } if ($page<$total_pages) { echo "&nbsp;<a href=\"" . $_SERVER["PHP_SELF"] . "?page=$olderpage\">Older &rarr;</a>"; } ?>

 
Output: