Site Navigation Code
  Home arrow Site Navigation Code arrow MySQL Paging Class
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
SITE NAVIGATION CODE

MySQL Paging Class
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 11
    2002-05-01

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Best Practices: The Integrated Project and Portfolio Management Platform.

    Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan.
    FREE! Go There Now!


    NEW! Accelerating Software Innovation on i on Power Systems

    Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time.
    FREE! Go There Now!


    NEW! Achieving True Agility -- How process can change the behavior of your tools

    Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools.
    FREE! Go There Now!


    NEW! Download a free trial of WebSphere Business Modeler Advanced V6.1.1

    Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement.
    FREE! Go There Now!


    NEW! IBM Rational AppScan Standard Edition V7.7

    Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7.
    FREE! Go There Now!


    NEW! Project and Portfolio Management Executive Resource Kit

    Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle.
    FREE! Go There Now!


    NEW! The dirty dozen: preventing common application-level hack attacks

    As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications.
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Functional Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications.
    FREE! Go There Now!


    NEW! Try IBM Rational Asset Manager V7.0 online!

    You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online.
    FREE! Go There Now!


    NEW! Whitepaper: Achieving consistency between business process models and operational guides

    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!

    SITE NAVIGATION CODE ARTICLES

    - Simple Menu System
    - Simply image viewer script
    - Simple File Lister
    - Dynamic Error Pages
    - BSoftEditor
    - Yahoo Status
    - Page numbers
    - PHP Search Navigator 1.0
    - Simple Page Navigation
    - An easy page browser ( prev 6 7 8 9 10 next )
    - AutoIndex PHP Script (Directory Indexer)
    - Bs_HtmlNavigation (Navigation and Sitemap cl...
    - Another Paging with Stage
    - Website Navigation via PHP
    - MySQL Paging Class





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT