Site Navigation Code

  Home arrow Site Navigation Code arrow MySQL Paging Class
SITE NAVIGATION CODE

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

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    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

    Developer Shed Affiliates

     



    © 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap