|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
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;
}
}
}
| | |
|
| Simple database class Categories : PHP, PHP Classes, MySQL, Databases | | | Link Manager for Link Exchangers Categories : PHP, PHP Classes, Databases, MySQL, CURL | | | A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server | | | This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | Simple Mini Poll class library (SimPoll) Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs | | | MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | | | Simple usersOnline class - keep track of how many users are online on your site Categories : PHP, PHP Classes, Databases, MySQL | | | PHP Object Example of the Perl DBI with MySQL Categories : PHP, PHP Classes, MySQL, Databases, Perl | | | PostGreSQL and MySQL 2 in 1 db Manager Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL | | | Password reminder Categories : PHP, PHP Classes, Databases, MySQL, Mail | | | 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 | | | [PHP5] aDB PDO LIKE Database Abstraction. Switch easily from one db server to another, strong errors management, manage transactions, queries preparation and more. Categories : PHP, PHP Classes, Databases, MS SQL Server, MySQL | |
| | | | 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->screen = $this->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->screen)) { $this->screen = 0; }
$this->pages = ceil($total_records / $rows_per_page);
$start = $this->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->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?
| |
|
|