WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation Click here to Update Your Picture
blacksnday bashmyex.com
Date : May 19th 2006
Grade : 4 of 5 (graded 2 times)
Viewed : 11251
File : 4402.php
Images : No Images for this code example.
Search : More code by blacksnday bashmyex.com
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

This Class uses my secure_url_param function which can be found at
http://www.weberdev.com/get_example-4400.html

Pagination of MYSQL results is a huge nightmare for many people!
It has been a constant headache for me until I finally forced
myself to write this Class.

This class will end ANY pagination fears you have.
it's Extremely Flexible and Easy to use.

What sets this Class apart from all others,
is that you can use up to 6 URL PARAMs
AND you can declare them ALL UP FRONT!

Please read the below code as it it fully documented on how to use.

/*       
        PAGEME VERSION 1.0

        BASHMYEX.COM PAGINATION CLASS TO
        DISPLAY PREV-NEXT LINKS ACROSS ALL OF BASHMYEX.COM
       
        WRITTEN BY:
        JOE F. (OWNER/DEVELOPER OF BASHMYEX.COM)
        Free to use for any purpose as long as the
        proper credits are given to the original author.
       

        IMPORTANT!!!
        The proper way to use this is to remember one
        VERY IMPORTANT THING:
            $pageme->bme_page_param_two();
        WILL ALWAYS BE USED AS THE MAIN PAGER SELECTION/COUNTER VALUE
        NO MATTER HOW MANY OTHER URL PARAMS YOU HAVE SET!!

        So if you only use ONE url Param... then you must use
        $pageme->bme_page_param_two(); to declare it!


        USAGE EXAMPLE:
        INITIATE CLASS AND DECLARE HOW LINKS ARE TO BE DISPLAYED:
            $pageme =& new bme_pagination("<-- View Previous |", "Article Page", "of", "| View Next -->");
            $pageme->bme_page_param_one('member');
            $pageme->bme_page_param_two('post');

            $query = "SELECT * FROM table WHERE foo='$boo' ";
            $result = $pageme->bme_paginate($query, $rows_per_page='10');
            ///    Don't forgot your sql while or other output statments here! ///
               
        Then display links by echoing them out:
            echo "
                $pageme->previous()
                $pageme->current()
                $pageme->next()
                ";
*/

class bme_pagination {


        /**
         * Constructor of the bme_pagination
         * If user defines what Links should read as, we define it here
         * Otherwise we make the defaults
         *
         * @param PHP_SELF: Should be enough said
         * @param prev:     What Previous link text should read as
         * @param current:  What Current Page link text should read as
         * @param of:       What Of(current page 1 OF 2) link text should read as
         * @param next:     What Next link text should read as
         */
        function bme_pagination($prev, $current, $of, $next)
        {
 
            $this->PHP_SELF = $_SERVER['PHP_SELF'];
       
            //set default links display or use user defined
                if(!$prev)
                {
                  $this->prev = "<- Previous Page |";
                  }else{
                    $this->prev = $prev;         
                    }
                   
                if(!$current)
                {
                  $this->current = "Current Page:";         
                  }else{
                    $this->current = $current;     
                    }
                   
                if(!$of)   
                {
                  $this->of = "of";
                  }else{
                    $this->of = $of;
                    }
                   
                if(!$next)   
                {
                  $this->next = "| Next Page ->";
                  }else{
                    $this->next = $next;
                    }
        }

        /**
         * $_GETs the first url param value
         * Then sets the first param
         *
         * @param param: The url param
         * example:      $param = bme_page_param_one('first')
         * would make:   first=value
         *
         * full url example:     http://bashmyex.com/index.php?first=1 
         */
        function bme_page_param_one($param)
        { 
              $this->get_param_one = secure_url_param($num, $nonum=$_GET[$param]);
              $this->param_one = "{$param}={$this->get_param_one}&";
          }

        /**
         * $_GETs the second url param value
         * Then sets the second param
         *
         * @param param: The url param
         * example:      $param = bme_page_param_two('second')
         * would make:    second=value
         *
         * full url example based on combing first and second:   
         * http://bashmyex.com/index.php?first=1&second=2 
         */
        function bme_page_param_two($param)
        {
              $this->param_two = $param;
              $this->get_param_two = secure_url_param($num, $nonum=$_GET[$this->param_two]);
        }


        /**
        * Sets optional 3rd, 4rth, 5th, 6th url params
        * While the above two bme_page_params allows for custom URL param's
        * They are limited and only allow to declare ONE PARAM each.
        *
        * This baby here allows you to create up to 4 seperate url Params
        * Giving a total of 6 possible URL Params from one Pagination Class!
        * USAGE WOULD BE LIKE: (will use above two to illustrate the purpose
        *
         * $pageme->bme_page_param_one('member');
        * $pageme->bme_page_param_two('post');
        * Would create and control a URL string LIKE:
        *    member=Blacksnday&post=0
        *   
        * Now what if you need more then just two URL Params?
        * WATCH THIS..............
        *
        * Create the first two just like above:
        *         $pageme->bme_page_param_one('member');
        *        $pageme->bme_page_param_two('post');
        * Now create this baby!
        *        $pageme->bme_page_param_three('cat', $param2, $param3, $param4);
        *            And with all there, your url could like something like:
        *                member=Blacksnday&cat=Media_Rants&post=0
        */
           
        function bme_page_param_three($param, $param2, $param3, $param4)
        {
   
              if($_GET[$param])
              {
                  $this->get_param_three_one = secure_url_param($num, $nonum=$_GET[$param], $type, $stype);
                  $this->param_three_one = "{$param}={$this->get_param_three_one}&";
                  }
     
              if($_GET[$param2])
              {   
                  $this->get_param_three_two = secure_url_param($num, $nonum=$_GET[$param2], $type, $stype);
                  $this->param_three_two = "{$param2}={$this->get_param_three_two}&";
                  }
     
              if($_GET[$param3])
              {
                  $this->get_param_three_three = secure_url_param($num, $nonum=$_GET[$param3], $type, $stype);
                  $this->param_three_three = "{$param3}={$this->get_param_three_three}&";
                  }
         
              if($_GET[$param4])
              {
                  $this->get_param_three_four = secure_url_param($num, $nonum=$_GET[$param4], $type, $stype);
                  $this->param_three_four = "{$param4}={$this->get_param_three_four}&";
                  }
        }
   
   
        function bme_paginate($query, $rows_per_page)
        {
 
            $result        = mysql_query($query);
            $this->screen  = $this->get_param_two;
            $total_records = mysql_num_rows($result);
       
                if (!$rows_per_page) { $rows_per_page = 10; }
                if (!isset($this->screen)) { $this->screen = 0; }
       
            $this->pages = ceil($total_records / $rows_per_page);
            $start       = $this->screen * $rows_per_page;
            $query      .= "LIMIT $start, $rows_per_page";
            $result      = mysql_query($query);

            return $result;
        }


        function previous()
        {
       
            if ($this->get_param_two > 0)
            {
                $j = $this->get_param_two - 1;
                $url = "{$siteurl}{$this->PHP_SELF}?
                    {$this->param_one}
                    {$this->param_three_one}
                    {$this->param_three_two}
                    {$this->param_three_three}
                    {$this->param_three_four}
                    {$this->param_two}={$j}#focus";
                   
                $nav = "<a href='$url'>{$this->prev}</a> ";
               
                return $nav;
            }

        }
   
   
        function current()
        {
   
            $p = 1;
            $lower = $p;
            $upper = $this->get_param_two+$p;
                while($upper>$this->pages)
                {
                    $p = $p-1;
                    $upper = $this->get_param_two+$p;
                }
           
            if($p<$lower)
            {
                $y = $lower-$p;
                $to = $this->get_param_two-$y;
                    while($to<0)
                    {
                        $to++;
                    }
            }
       
            for ($i=$this->get_param_two;$i<$upper;$i++)
            {
                $url = "{$siteurl}{$this->PHP_SELF}?
                    {$this->param_one}
                    {$this->param_three_one}
                    {$this->param_three_two}
                    {$this->param_three_three}
                    {$this->param_three_four}";
                   
                $j = $i + 1;
                $n = $j - 1;
                $nav = "<a href='{$url}{$this->param_two}={$n}'>{$this->current} {$j} {$this->of} {$this->pages}</a>"; 
       
                return $nav;
            }
         }       


        function next()
        {

            if ($this->screen < $this->pages-1)
            {
                $j = $this->get_param_two + 1;
                $url = "{$siteurl}{$this->PHP_SELF}?
                        {$this->param_one}
                        {$this->param_three_one}
                        {$this->param_three_two}
                        {$this->param_three_three}
                        {$this->param_three_four}
                        {$this->param_two}={$j}#focus";
                   
                $nav = "<a href='$url'>{$this->next}</a>";
               
                return $nav;
            } 
           
        }

}



usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
Record Set Paging with PHP (RSP)
Categories : PHP, MySQL, Navigation, Databases, HTML and PHP
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
Simple database class
Categories : PHP, PHP Classes, MySQL, Databases
Simple usersOnline class - keep track of how many users are online on your site
Categories : PHP, PHP Classes, Databases, MySQL
Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP.
Categories : PHP Classes, Databases, PHP, MySQL, InnoDB
YellowPages Content Grabber (PHP5 +)
Categories : PHP, PHP Classes, Regexps, Databases, MySQL
Ajax PHP Tree (Left and Right) with MySQL
Categories : PHP, Databases, MySQL, AJAX, PHP Classes
Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible
Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server
MySQL Connection/Query Class
Categories : Databases, MySQL, PHP, PHP Classes
 Jim Lucas wrote : 1639
My only suggestion is that you learn to use isset() and !empty():

Demonstration:
function bme_paginate($query, $rows_per_page)
{
 
  $result        = mysql_query($query);
  $this-&gt;screen  = $this-&gt;get_param_two;
  $total_records = mysql_num_rows($result);
       
  /*
  Why are you checking this?  If it is not set, your script will crash with a Fatal Error because the param is required for the function?  
  Plus you should also be checking to see if the value is numeric, if not, then it is not valid also.  The in-secure thing is, is that you are using $rows_per_page right in the SQL statement without any obvious validation of input.
  */
    if (!$rows_per_page) { $rows_per_page = 10; }
    if (!isset($this-&gt;screen)) { $this-&gt;screen = 0; }
       
  $this-&gt;pages = ceil($total_records / $rows_per_page);
  $start       = $this-&gt;screen * $rows_per_page;
  $query      .= "LIMIT $start, $rows_per_page";
/* Rather than running the SELECT with the LIMIT added here.  Doubling your work that is done.  Since you know your limit on the out side of this class.  Why not use mysql_data_seek() instead.
*/

  $result      = mysql_query($query);

  return $result;


Just my $0.02`s worth.
 
 blacksnday bashmyex.com wrote : 1640
As I already point out in my code comments:

 IMPORTANT!!! 
        The proper way to use this is to remember one 
        VERY IMPORTANT THING: 
            $pageme-&gt;bme_page_param_two(); 
        WILL ALWAYS BE USED AS THE MAIN PAGER SELECTION/COUNTER VALUE 
        NO MATTER HOW MANY OTHER URL PARAMS YOU HAVE SET!! 

Any pagination NEEDS a set value that will be used to
determine what to display next.
Read the comments in the code and you will see this.

As for $rows_per_page needing numeric validation.
I figure that anyone who uses this script will not
attempt to hack/crash their own website.

Even so, to soothe people like yourself, you can easily add the already used secure_url_param().

Using isset() and !empty().. personal preferences are usually just that.
 
 blacksnday bashmyex.com wrote : 1647
This pagination class will be updated eventually and if interested you can always grab the latest CVS release located at:

http://dev.bmescripts.com/index.cgi/opensource/dirview?d=opensource

I only allow browser based access, so you can`t grab updates from a CVS client
 
 Boaz Yahav wrote :1694
Your site seems to be down.

I`m trying to get rid of all the PHP Notices this class throws. Is there a newer version?