- /**
- * filename:ext_page.class.php
- * @package:p Hpbean
- * Descrīption: Ultra-strong paging class, four paging modes, default to a baidu,google-like paging style.
- * 2.0 added Features: Support custom style, custom style, support PHP4 and PHP5,
- * 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);
- Turn on Ajax:
- $ajaxpage =new page (Array (' Total ' =>1000, ' perpage ' =>20, ' ajax ' = ' ajax_page ', ' page_name ' = ' test '));
- Echo ' Mode:1
'. $ajaxpage->show ();
- Takes the inherited custom paging display mode.
- Editing: Scripting Academy http://bbs.it-home.org
- */
- Class _page
- {
- /**
- * Config, public
- */
- var $page _name= "Pb_page";//page tag, used to control the URL page. such as xxx.php? The Pb_page in pb_page=2
- var $next _page= ' > ';//Next page
- var $pre _page= ' < ';//prev
- var $first _page= ' first ';//Home
- var $last _page= ' last ';//End
- var $pre _bar= ' << ';//previous page bar
- var $next _bar= ' >> ';//Next page Bar
- var $format _left= ' [';
- var $format _right= '];
- var $is whether _ajax=false;//supports Ajax paging mode
- /**
- * Private
- *
- */
- var $pagebarnum =10;//Control the number of record bars.
- var $totalpage total Pages =0;//
- var $ajax _action_name= ";//ajax action Name
- var $nowindex =1;//Current Page
- var $url = "";//url address Header
- var $offset = 0;
- /**
- * Constructor 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);//Set the current page
- $this->_set_url ($url);//Set link address
- $this->totalpage=ceil ($total/$perpage);
- $this->offset= ($this->nowindex-1) * $perpage;
- if (!empty ($array [' Ajax])) $this->open_ajax ($array [' Ajax ']);//Open Ajax mode
- }
- /**
- * Set the value of the variable name specified in the class, if the change does not belong to this class, will throw a 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!");
- }
- }
- /**
- * Turn on 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 shows "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.";
- }
- /**
- * Get the code that shows "previous page"
- *
- * @param string $style
- * @return String
- */
- function Pre_page ($style = ")
- {
- if ($this->nowindex>1) {
- return $this->_get_link ($this->_get_url ($this->nowindex-1), $this->pre_page, $style);
- }
- Return ". $this->pre_page.";
- }
- /**
- * Get the code that shows "home page"
- *
- * @return String
- */
- function First_page ($style = ")
- {
- if ($this->nowindex==1) {
- Return ". $this->first_page.";
- }
- return $this->_get_link ($this->_get_url (1), $this->first_page, $style);
- }
- /**
- * Get the code that shows "last"
- *
- * @return String
- */
- function Last_page ($style = ")
- {
- if ($this->nowindex== $this->totalpage) {
- Return ". $this->last_page.";
- }
- return $this->_get_link ($this->_get_url ($this->totalpage), $this->last_page, $style);
- }
- function Nowbar ($style = ", $nowindex _style=")
- {
- $plus =ceil ($this->PAGEBARNUM/2);
- if ($this->pagebarnum-$plus + $this->nowindex> $this->totalpage) $plus = ($this->pagebarnum-$this totalpage+ $this->nowindex);
- $begin = $this->nowindex-$plus +1;
- $begin = ($begin >=1)? $begin: 1;
- $return = ";
- for ($i = $begin; $i < $begin + $this->pagebarnum; $i + +)
- {
- if ($i <= $this->totalpage) {
- if ($i! = $this->nowindex)
- $return. = $this->_get_text ($this->_get_link ($this->_get_url ($i), $i, $style));
- Else
- $return. = $this->_get_text (". $i. ');
- }else{
- Break
- }
- $return. = "\ n";
- }
- Unset ($begin);
- return $return;
- }
- /**
- * Get the code to show the Jump button
- *
- * @return String
- */
- function Select ()
- {
- $return = ''; for ($i =1; $i <= $this->totalpage; $i + +) {if ($i = = $this->nowindex) {$return. = ''. $i. '; }else{$return. = ''. $i. '; }} unset ($i); $return. = '';
- return $return;
- }
- /**
- * Get the value required for limit in MySQL statement
- *
- * @return String
- */
- function offset ()
- {
- return $this->offset;
- }
- /**
- * Control the pagination display style (you can add the corresponding style)
- *
- * @param int $mode
- * @return String
- */
- Function Show ($mode =1)
- {
- Switch ($mode)
- {
- Case ' 1 ':
- $this->next_page= ' next page ';
- $this->pre_page= ' previous page ';
- return $this->pre_page (). $this->nowbar (). $this->next_page (). ' Section '. $this->select (). ' Page ';
- Break
- Case ' 2 ':
- $this->next_page= ' next page ';
- $this->pre_page= ' previous page ';
- $this->first_page= ' home ';
- $this->last_page= ' last ';
- return $this->first_page (). $this->pre_page (). ' [No. $this->nowindex] '. $this->next_page (). $this->last_page (). ' Section '. $this->select (). ' Page ';
- Break
- Case ' 3 ':
- $this->next_page= ' next page ';
- $this->pre_page= ' previous page ';
- $this->first_page= ' home ';
- $this->last_page= ' last ';
- return $this->first_page (). $this->pre_page (). $this->next_page (). $this->last_page ();
- Break
- Case ' 4 ':
- $this->next_page= ' next page ';
- $this->pre_page= ' previous page ';
- return $this->pre_page (). $this->nowbar (). $this->next_page ();
- Break
- Case ' 5 ':
- return $this->pre_bar (). $this->pre_page (). $this->nowbar (). $this->next_page (). $this->next_bar ();
- Break
- Case ' 6 ':
- return $this->select ();
- Break
- Case ' 7 ':
- return $this->nowbar ();
- Break
- }
- }
- /*----------------Private Function (proprietary method)-----------------------------------------------------------*/
- /**
- * Set 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 get
- if (Empty ($_server[' query_string ')) {
- When there is no query_string
- $this->url=$_server[' Request_uri ']. "?". $this->page_name. " =";
- }else{
- //
- if (Stristr ($_server[' query_string '), $this->page_name. ' =')){
- Address exists page parameter
- $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 Current page
- *
- */
- function _set_nowindex ($nowindex)
- {
- if (empty ($nowindex)) {
- System acquisition
- if (Isset ($_get[$this->page_name])) {
- $this->nowindex=intval ($_get[$this->page_name]);
- }
- if (Isset ($_post[' Pb_page_select ')) {
- $this->nowindex=$_post[' Pb_page_select '];
- }
- }else{
- Manually set
- $this->nowindex=intval ($nowindex);
- }
- }
- /**
- * Returns the address value for the specified page
- *
- * @param int $pageno
- * @return String $url
- */
- function _get_url ($pageno =1)
- {
- Return $this->url. $pageno;
- }
- /**
- * Get pagination display text, e.g. _get_text (' 1 ') will return by default [1]
- *
- * @param String $str
- * @return String $url
- */
- function _get_text ($STR)
- {
- Return $this->format_left. $str. $this->format_right;
- }
- /**
- * Get link address
- */
- function _get_link ($url, $text, $style = ") {
- $style = (Empty ($style))? ": ' class=" '. $style. ' ";
- if ($this->is_ajax) {
- If you are using AJAX mode
- Return ' Ajax_action_name. ' (\ '. $url. ') \ ') ' > ' $text. ';
- }else{
- Return '. $text. ';
- }
- }
- /**
- * Error Handling method
- */
- Function error ($function, $errormsg)
- {
- Die (' Error in File '. __file__ ' , Function '. $function. ' () : '. $errormsg);
- }
- }
- Inherit the paging class and join the database access capability.
- Class Page extends _page {
- var $db; DB Connected Object
- var $_sql_query = '; Querying the SQL for a database
- var $_total = 0; The total record that was queried. Must first
- var $_rst = array (); The record that was queried.
- /**
- * Paged Query class library.
- *
- * @param String $SQL The SQL statement that records the query.
- * @param int $pagenuber How many records per page.
- * @param int $pagen the current page.
- * @param String $url The parameters that are included in the paging link. Index.php?xx=b&bb=33
- * @param String $pname The current page of markup, default is index.php?xx=b&bb=33&page=2 if there are special requirements
- You can modify the parameters of the $pname. For example: $pname = ' db_page ', it becomes: index.php?xx=b&bb=33&db_page=2
- * @return Mysql_page
- */
- function Page ($db, $sql _query = ", $max _rows_per_page = $, $current _page_number = 0, $url = ', $parameters = ', $pname = ' Pb_page ', $OTC = ' * ') {
- $this db = $db;
- $pos _to = strlen ($sql _query);
- $pos _from = Strpos ($sql _query, ' from ', 0);
- $pos _group_by = Strpos ($sql _query, ' GROUP by ', $pos _from);
- if ($pos _group_by < $pos _to) && ($pos _group_by! = False)) $pos _to = $pos _group_by;
- $pos _having = Strpos ($sql _query, ' having ', $pos _from);
- if ($pos _having < $pos _to) && ($pos _having! = False)) $pos _to = $pos _having;
- $pos _order_by = Strpos ($sql _query, ' ORDER by ', $pos _from);
- if ($pos _order_by < $pos _to) && ($pos _order_by! = False)) $pos _to = $pos _order_by;
- $reviews _count = $this, db, GetResults ("SELECT count ($OTC) as Total". substr ($sql _query, $pos _from, ($pos _to-$pos _from));
- $query _num_rows = $reviews _count[0][' total ');
- $this-_total = $query _num_rows;
- $num _pages = ceil ($query _num_rows/$max _rows_per_page);
- if ($current _page_number > $num _pages) {
- $current _page_number = $num _pages;
- }
- $offset = ($max _rows_per_page * ($current _page_number-1));
- if ($offset < 0) $offset = 0;
- if ($offset > 0) {
- $offset = $offset + 1;
- }
- $this-_sql_query = $sql _query. "Limit". $offset. ", " . $max _rows_per_page;
- $this-SetData (); Query the database.
- Parent:: page (' total ' = $query _num_rows, ' perpage ' = = $max _rows_per_page, ' page_name ' + = $pname, ' url ' =& Gt $url, ' parameters ' = $parameters));
- }
- /**
- * Gets a record of the current page, returning an array.
- */
- function Findbyall () {
- return $this, _rst;
- }
- /**
- * Display paging information.
- *
- * @param int $model
- */
- function Dispaly_links ($model) {
- $this, Show ($model);
- }
- /**
- * Returns the number of records.
- *
- * @return Int
- */
- function GetCount () {
- return $this, _total;
- }
- /**
- * Fetch query result record number.
- *
- * @return Int
- */
- function GetRows () {
- Return count ($this-_rst);
- }
- /**
- * Perform query function.
- * Computes an array.
- * Private method.
- */
- function SetData () {
- $this-_rst = $this, db, GetResults ($this, _sql_query);
- }
- }
- ?>
Copy Code |