A class that makes paging (previous and next links) through MySQL results easy.
By : Jesse
<?php
/**************************************************************************************
* Class: Pager
* Author: Tsigo <tsigo@tsiris.com>
* Methods:
* findStart
* findPages
* pageList
* nextPrev
* Redistribute as you see fit.
**************************************************************************************/
class Pager
{
/***********************************************************************************
* int findStart (int limit)
* Returns the start offset based on $_GET['page'] and $limit
***********************************************************************************/
function findStart($limit)
{
if ((!isset($_GET['page'])) || ($_GET['page'] == "1"))
{
$start = 0;
$_GET['page'] = 1;
}
else
{
$start = ($_GET['page']-1) * $limit;
}
return $start;
}
/***********************************************************************************
* int findPages (int count, int limit)
* Returns the number of pages needed based on a count and a limit
***********************************************************************************/
function findPages($count, $limit)
{
$pages = (($count % $limit) == 0) ? $count / $limit : floor($count / $limit) + 1;
return $pages;
}
/***********************************************************************************
* string pageList (int curpage, int pages)
* Returns a list of pages in the format of "« < [pages] > »"
***********************************************************************************/
function pageList($curpage, $pages)
{
$page_list = "";
/* Print the first and previous page links if necessary */
if (($curpage != 1) && ($curpage))
{
$page_list .= " <a href=\"".$_SERVER['PHP_SELF']."?page=1\" title=\"First Page\">«</a> ";
}
if (($curpage-1) > 0)
{
$page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."\" title=\"Previous Page\"><</a> ";
}
/* Print the numeric page list; make the current page unlinked and bold */
for ($i=1; $i<=$pages; $i++)
{
if ($i == $curpage)
{
$page_list .= "<b>".$i."</b>";
}
else
{
$page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".$i."\" title=\"Page ".$i."\">".$i."</a>";
}
$page_list .= " ";
}
/* Print the Next and Last page links if necessary */
if (($curpage+1) <= $pages)
{
$page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage+1)."\" title=\"Next Page\">></a> ";
}
if (($curpage != $pages) && ($pages != 0))
{
$page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".$pages."\" title=\"Last Page\">»</a> ";
}
$page_list .= "</td>\n";
return $page_list;
}
/***********************************************************************************
* string nextPrev (int curpage, int pages)
* Returns "Previous | Next" string for individual pagination (it's a word!)
***********************************************************************************/
function nextPrev($curpage, $pages)
{
$next_prev = "";
if (($curpage-1) <= 0)
{
$next_prev .= "Previous";
}
else
{
$next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."\">Previous</a>";
}
$next_prev .= " | ";
if (($curpage+1) > $pages)
{
$next_prev .= "Next";
}
else
{
$next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage+1)."\">Next</a>";
}
return $next_prev;
}
}
?>
Example on how to use:
<?php
/* Instantiate class */
require_once("class.pager.php");
$p = new Pager;
/* Show many results per page? */
$limit = 100;
/* Find the start depending on $_GET['page'] (declared if it's null) */
$start = $p->findStart($limit);
/* Find the number of rows returned from a query; Note: Do NOT use a LIMIT clause in this query */
$count = mysql_num_rows(mysql_query("SELECT field FROM table"));
/* Find the number of pages based on $count and $limit */
$pages = $p->findPages($count, $limit);
/* Now we use the LIMIT clause to grab a range of rows */
$result = mysql_query("SELECT * FROM table LIMIT ".$start.", ".$limit);
/* Now get the page list and echo it */
$pagelist = $p->pageList($_GET['page'], $pages);
echo $pagelist;
/* Or you can use a simple "Previous | Next" listing if you don't want the numeric page listing */
//$next_prev = $p->nextPrev($_GET['page'], $pages);
//echo $next_prev;
/* From here you can do whatever you want with the data from the $result link. */
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Site Navigation Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Poor Requirements Management capabilities in an Enterprise have been linked to excessive project failures, escalating IT costs, and failure to deliver competitive advantage into the marketplace. Join Brianna M Smith from IBM Rational and learn about how successful organizations align IT and Business stakeholders through collaborative processes and tools for effective requirements management, and how an integrated approach across the IT lifecycle can provide unparalleled visibility and traceability to ensure that project teams are delivering on the business vision by "doing the right things" and "doing things right." FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems. FREE! Go There Now!
|
|
|
|
Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
|
|
|
|
Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services. FREE! Go There Now!
|
|
|
|
Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase. FREE! Go There Now!
|
|
|
|
This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”. FREE! Go There Now!
|
|
|
|
In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
|
|
|
|
Explore how Rational and WebSphere software enable enterprise documentation in SOA environments. Specifically, a new integration between IBM WebSphere® Business Modeler and IBM Rational® Method Composer software can help technical writers more easily keep enterprise operations manuals in sync with changes that are made to business processes, resulting in more accurate and timely documentation that benefits the entire enterprise. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |