- /**
- * Filename: ext_page.class.php
- * @ Package: phpbean
- * Descr paging ption: powerful paging mode. it adopts four paging modes, which are similar to baidu and google by default.
- * 2.0 added: supports custom styles and styles, and supports both 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 );
- 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.
- Edit: Script school http://bbs.it-home.org
- */
- Class _ page
- {
- /**
- * Config, public
- */
- Var $ page_name = "PB_page"; // page label, used to control url pages. For example, xxx. php? PB_page = PB_page in 2
- 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 .'';
- }
-
- /**
- * Get the code for displaying the "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 for displaying the "homepage"
- *
- * @ 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 for displaying the "last page"
- *
- * @ 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 for displaying 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;
- }
-
- /**
- * Obtain the limit value in the mysql statement.
- *
- * @ Return string
- */
- Function offset ()
- {
- Return $ this-> offset;
- }
-
- /**
- * Control the display style by page (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 (). 'Di '. $ this-> select (). 'Page ';
- Break;
- Case '2 ':
- $ This-> next_page = 'next page ';
- $ This-> pre_page = 'previous page ';
- $ This-> first_page = 'homepage ';
- $ This-> last_page = 'Last page ';
- Return $ this-> first_page (). $ this-> pre_page (). '[nth '. $ this-> nowindex. 'page] '. $ this-> next_page (). $ this-> last_page (). 'Di '. $ this-> select (). 'Page ';
- Break;
- Case '3 ':
- $ This-> next_page = 'next page ';
- $ This-> pre_page = 'previous page ';
- $ This-> first_page = 'homepage ';
- $ This-> last_page = 'Last page ';
- 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 (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]);
- }
- If (isset ($ _ POST ['pb _ Page_Select ']) {
- $ This-> nowindex =$ _ POST ['pb _ Page_Select '];
- }
- } 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 );
- }
- }
-
- // Inherit the paging class and add the database access capability.
- Class Page extends _ Page {
- Var $ db; // db connected object
- Var $ _ SQL _Query = ''; // query the database SQL
- Var $ _ Total = 0; // The Total number of queried records.
- Var $ _ Rst = array (); // query records.
- /**
- * The paging query class library.
- *
- * @ Param String $ the SQL statement used to query SQL records.
- * @ Param int $ pagenuber: the number of records per page.
- * @ Param int $ pagen current page.
- * @ Param String $ parameter. index. php? Xx = B & bb = 33
- * @ Param String $ tag of the current page of pname. the default value is index. php? Xx = B & bb = 33 & page = 2 if you have special requirements
- You can modify the $ pname parameter. for example, $ pname = 'DB _ page' is changed to index. php? Xx = B & bb = 33 & db_page = 2
- * @ Return Mysql_Page
- */
-
- Function Page ($ db, $ SQL _query = '', $ max_rows_per_page = 20, $ 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 (array ('total' => $ query_num_rows, 'perpage' => $ max_rows_per_page, 'page _ name' => $ pname, 'URL' => $ url, 'parameters '=> $ parameters ));
- }
-
- /**
- * Get the record of the current page and return an array.
- */
- Function findByAll (){
- Return $ this-> _ Rst;
- }
- /**
- * Display page information.
- *
- * @ Param int $ model
- */
- Function dispaly_links ($ model ){
- $ This-> show ($ model );
- }
- /**
- * Number of returned Records.
- *
- * @ Return Int
- */
- Function getCount (){
- Return $ this-> _ Total;
- }
- /**
- * Number of query result records ..
- *
- * @ Return Int
- */
- Function getRows (){
- Return count ($ this-> _ Rst );
- }
- /**
- * Execute the query function.
- * Calculate the array.
- * Private method.
- */
- Function setData (){
- $ This-> _ Rst = $ this-> db-> getResults ($ this-> _ SQL _Query );
- }
- }
-
- ?>
|