Recently, a friend asked me if I could write a php paging class. I thought about it carefully. In fact, I haven't touched this thing for a while. let's talk about some experience, it's okay to write something simple. if it's too complicated to write, it's a bit difficult. let's take a look at this piece of code:
PHP
The code is as follows:
/**
* Filename: ext_page.class.php
* @ Package: phpbean
* @ Author: feifengxlq * @ Copyright: Copyright 2006 feifengxlq
* @ License: version 2.0
* @ Create: 2006-5-31
* @ Modify: 2006-6-1
* @ Modify: feifengxlq 2006-11-4
* Description: Paging type. four paging modes are available.
* 2.0 added: supports custom styles and styles, and supports both PHP4 and PHP5,
* To see detail, please visit http://www.phpobject.net/blog/read.php?
* Example:
* Mode: four paging modes:
Require_once ('../libs/classes/page. class. php ');
$ Page = new page (array ('total' => 1000, 'perpage' => 20 ));
Echo 'MoDe: 1
'. $ Page-> show ();
Echo 'MoDe: 2
'. $ Page-> show (2 );
Echo 'MoDe: 3
'. $ Page-> show (3 );
Echo 'MoDe: 4
'. $ Page-> show (4 );
Enable AJAX:
$ Ajaxpage = new page (array ('total' => 1000, 'perpage' => 20, 'Ajax '=> 'Ajax _ page ', 'page _ name' => 'test '));
Echo 'MoDe: 1
'. $ Ajaxpage-> show ();
Use the inherited custom paging display mode:
Demo: http://www.phpobject.net/blog
*/
Class page
{
/**
* Config, public
*/
Var $ page_name = "PB_page"; // page tag, used to control url pages
Var $ next_page = '>'; // Next page
Var $ pre_page = '<'; // Previous Page
Var $ first_page = 'first'; // home page
Var $ last_page = 'Lala'; // The Last page.
Var $ pre_bar = '<'; // The Last Page
Var $ next_bar = '>'; // Next page
Var $ format_left = '[';
Var $ format_right = ']';
Var $ is_ajax = false; // whether the AJAX paging mode is supported
/**
* Private
*
*/
Var $ pagebarnum = 10; // control the number of records.
Var $ totalpage = 0; // total number of pages
Var $ ajax_action_name = ''; // AJAX action name
Var $ nowindex = 1; // current page
Var $ url = ""; // url header
Var $ offset = 0;
/**
* Constructor
*
* @ Param array $ array ['total'], $ array ['perpage'], $ array ['nowindex '], $ array ['URL'], $ array ['Ajax ']
*/
Function page ($ array)
{
If (is_array ($ array )){
If (! Array_key_exists ('total', $ array) $ this-> error (_ FUNCTION __, 'need a param of total ');
$ Total = intval ($ array ['total']);
$ Perpage = (array_key_exists ('perpage', $ array ))? Intval ($ array ['perpage']): 10;
$ Nowindex = (array_key_exists ('nowindex ', $ array ))? Intval ($ array ['nowindex ']): '';
$ Url = (array_key_exists ('URL', $ array ))? $ Array ['URL']: '';
} Else {
$ Total = $ array;
$ Perpage = 10;
$ Nowindex = '';
$ Url = '';
}
If ((! Is_int ($ total) | ($ total <0) $ this-> error (_ FUNCTION __, $ total. 'is not a positive integer! ');
If ((! Is_int ($ perpage) | ($ perpage <= 0) $ this-> error (_ FUNCTION __, $ perpage. 'is not a positive integer! ');
If (! Empty ($ array ['page _ name']) $ this-> set ('page _ name', $ array ['page _ name']); // set pagename
$ This-> _ set_nowindex ($ nowindex); // you can specify the current page.
$ This-> _ set_url ($ url); // sets the link address.
$ This-> totalpage = ceil ($ total/$ perpage );
$ This-> offset = ($ this-> nowindex-1) * $ perpage;
If (! Empty ($ array ['Ajax ']) $ this-> open_ajax ($ array ['Ajax']); // enable the ajax mode
}
/**
* Set the value of the variable name specified in the class. if the change volume does not belong to this class, throw an exception
*
* @ Param string $ var
* @ Param string $ value
*/
Function set ($ var, $ value)
{
If (in_array ($ var, get_object_vars ($ this )))
$ This-> $ var = $ value;
Else {
$ This-> error (_ FUNCTION __, $ var. "does not belong to PB_Page! ");
}
}
/**
* Open the inverted AJAX mode.
*
* @ Param string $ action the default ajax-triggered action.
*/
Function open_ajax ($ action)
{
$ This-> is_ajax = true;
$ This-> ajax_action_name = $ action;
}
/**
* Get the code that displays the next page.
*
* @ Param string $ style
* @ Return string
*/
Function next_page ($ style = '')
{
If ($ this-> nowindex <$ this-> totalpage ){
Return $ this-> _ get_link ($ this-> _ get_url ($ this-> nowindex + 1), $ this-> next_page, $ style );
}
Return ''. $ this-> next_page .'';
}
}
/* ---------------- Private function (private method )-----------------------------------------------------------*/
/**
* Set the url header address
* @ Param: String $ url
* @ Return boolean
*/
Function _ set_url ($ url = "")
{
If (! Empty ($ url )){
// Manually set
$ This-> url = $ url. (stristr ($ url ,'? '))? '&':'? '). $ This-> page_name. "= ";
} Else {
// Automatically obtain
If (empty ($ _ SERVER ['query _ string']) {
// When QUERY_STRING does not exist
$ This-> url = $ _ SERVER ['request _ URI ']. "? ". $ This-> page_name." = ";
} Else {
//
If (stristr ($ _ SERVER ['query _ string'], $ this-> page_name. '= ')){
// The address has page parameters.
$ This-> url = str_replace ($ this-> page_name. '='. $ this-> nowindex, '', $ _ SERVER ['request _ URI ']);
$ Last = $ this-> url [strlen ($ this-> url)-1];
If ($ last = '? '| $ Last = '&'){
$ This-> url. = $ this-> page_name. "= ";
} Else {
$ This-> url. = '&'. $ this-> page_name. "= ";
}
} Else {
//
$ This-> url = $ _ SERVER ['request _ URI '].' & '. $ this-> page_name.' = ';
} // End if
} // End if
} // End if
}
/**
* Set the current page
*
*/
Function _ set_nowindex ($ nowindex)
{
If (empty ($ nowindex )){
// System retrieval
If (isset ($ _ GET [$ this-> page_name]) {
$ This-> nowindex = intval ($ _ GET [$ this-> page_name]);
}
} Else {
// Manually set
$ This-> nowindex = intval ($ nowindex );
}
}
/**
* Return the address value for the specified page.
*
* @ Param int $ pageno
* @ Return string $ url
*/
Function _ get_url ($ pageno = 1)
{
Return $ this-> url. $ pageno;
}
/**
* Get the text displayed by page. for example, by default, _ get_text ('1') will return [1].
*
* @ Param String $ str
* @ Return string $ url
*/
Function _ get_text ($ str)
{
Return $ this-> format_left. $ str. $ this-> format_right;
}
/**
* Obtain the link address
*/
Function _ get_link ($ url, $ text, $ style = ''){
$ Style = (empty ($ style ))? '': 'Class =" '. $ style .'"';
If ($ this-> is_ajax ){
// If the AJAX mode is used
Return 'Ajax _ action_name. '(\ ''. $ url.' \ ')">'. $ text .'';
} Else {
Return ''. $ text .'';
}
}
/**
* Error handling method
*/
Function error ($ function, $ errormsg)
{
Die ('error in file
'. _ FILE __.', Function
'. $ Function .'(): '. $ Errormsg );
}
}
$ Page = new page (array ('total' => 1000, 'perpage' => 20 ));
Echo 'MoDe: 1
'. $ Page-> show ();
Echo 'MoDe: 2
'. $ Page-> show (2 );
Echo 'MoDe: 3
'. $ Page-> show (3 );
Echo 'MoDe: 4
'. $ Page-> show (4 );
Echo 'start AJAX mode :';
$ Ajaxpage = new page (array ('total' => 1000, 'perpage' => 20, 'Ajax '=> 'Ajax _ page ', 'page _ name' => 'test '));
Echo 'MoDe: 1
'. $ Ajaxpage-> show ();
?>
In fact, my current job is to use the telephone recording system software such as telephone recording cards. it is very sensitive to some code, not just to learn php, but to learn more about any programming language, write more, think more!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.