<? Php /*************************************** ****** Class Name: PageSupport Function: displays data in the MySQL database by page. **************************************** *******/ Class PageSupport { // Attributes Var $ SQL; // SQL query statement of the data to be displayed Var $ page_size; // maximum number of lines per page Var $ start_index; // the sequence number of the first row of the record to be displayed Var $ total_records; // The total number of records. Var $ current_records; // number of records read on the current page Var $ result; // read the result Var $ total_pages; // total number of pages Var $ current_page; // current page number Var $ display_count = 30; // The first and last pages displayed Var $ arr_page_query; // array, which contains the parameters to be passed for pagination Var $ first; Var $ prev; Var $ next; Var $ last; // Method /*************************************** ****** Construct () Input parameters: $ Ppage_size: Maximum number of lines per page **************************************** *******/ Function PageSupport ($ ppage_size) { $ This-> page_size = $ ppage_size; $ This-> start_index = 0; } /*************************************** ****** Constructor :__ destruct () Input parameters: **************************************** *******/ Function _ destruct () {
} /*************************************** ****** Get function :__ get () **************************************** *******/ Function _ get ($ property_name) { If (isset ($ this-> $ property_name )) { Return ($ this-> $ property_name ); } Else { Return (NULL ); } } /*************************************** ****** Set function :__ set () **************************************** *******/ Function _ set ($ property_name, $ value) { $ This-> $ property_name = $ value; } /*************************************** ****** Function Name: read_data Function: reads records from a table based on SQL query statements. Returned value: Two-Dimensional attribute array result [Record Number] [field name] **************************************** *******/ Function read_data () { $ Psql = $ this-> SQL; // Query data, database links, and other information should be implemented outside class calls $ Result = mysql_query ($ psql) or die (mysql_error ()); $ This-> total_records = mysql_num_rows ($ result ); // Use the LIMIT keyword to obtain the record to be displayed on this page If ($ this-> total_records> 0) { $ This-> start_index = ($ this-> current_page-1) * $ this-> page_size; $ Psql = $ psql. "LIMIT". $ this-> start_index. ",". $ this-> page_size; $ Result = mysql_query ($ psql) or die (mysql_error ()); $ This-> current_records = mysql_num_rows ($ result ); // Put the query result in the result Array $ I = 0; While ($ row = mysql_fetch_Array ($ result )) { $ This-> result [$ I] = $ row; $ I ++; } } // Obtain the total number of pages and current page information $ This-> total_pages = ceil ($ this-> total_records/$ this-> page_size );
$ This-> first = 1; $ This-> prev = $ this-> current_page-1; $ This-> next = $ this-> current_page + 1; $ This-> last = $ this-> total_pages; } /*************************************** ****** Function Name: standard_navigate () Function: displays the home page, next page, last page, and not pages. **************************************** *******/ Function standard_navigate () { Echo "<div align = center> "; Echo "<form action =". $ _ SERVER ['php _ SELF ']. "method =" get "> "; Echo "<font color = red size = '4'> No ". $ this-> current_page. "Page/total ". $ this-> total_pages. "page </font> "; Echo ""; Echo "Jump to the <input type =" text "size = regular" name = "current_page" value = '". $ this-> current_page."'/> page "; Echo "<input type =" submit "value =" submit "/> "; // Generate a navigation link If ($ this-> current_page> 1 ){ Echo "<A href =". $ _ SERVER ['php _ SELF ']. "? Current_page = ". $ this-> first."> homepage </A> | "; Echo "<A href =". $ _ SERVER ['php _ SELF ']. "? Current_page = ". $ this-> prev."> previous page </A> | "; }
If ($ this-> current_page <$ this-> total_pages ){ Echo "<A href =". $ _ SERVER ['php _ SELF ']. "? Current_page = ". $ this-> next."> next page </A> | "; Echo "<A href =". $ _ SERVER ['php _ SELF ']. "? Current_page = ". $ this-> last."> last page </A> "; } Echo "</form> "; Echo "</div> "; } /*************************************** ****** Function Name: full_navigate () Function: displays the home page, next page, last page, and not pages. Generate navigation links such as 1 2 3... 10 11 **************************************** *******/ Function full_navigate () { Echo "<div align = center> "; Echo "<form action =". $ _ SERVER ['php _ SELF ']. "method =" get "> "; Echo "<font color = red size = '4'> No ". $ this-> current_page. "Page/total ". $ this-> total_pages. "page </font> "; Echo ""; Echo "Jump to the <input type =" text "size = regular" name = "current_page" value = '". $ this-> current_page."'/> page "; Echo "<input type =" submit "value =" submit "/> "; // Generate a navigation link such as 1 2 3... 10 11 $ Front_start = 1; If ($ this-> current_page> $ this-> display_count ){ $ Front_start = $ this-> current_page-$ this-> display_count; } For ($ I = $ front_start; $ I <$ this-> current_page; $ I ++ ){ Echo "<a href =". $ _ SERVER ['php _ SELF ']. "? Page = ". $ I."> [". $ I."] </a> "; } Echo "[". $ this-> current_page. "]"; $ DisplayCount = $ this-> display_count; If ($ this-> total_pages> $ displayCount & ($ this-> current_page + $ displayCount) <$ this-> total_pages ){ $ DisplayCount = $ this-> current_page + $ displayCount; } Else { $ DisplayCount = $ this-> total_pages; } For ($ I = $ this-> current_page + 1; $ I <= $ displayCount; $ I ++ ){ Echo "<a href =". $ _ SERVER ['php _ SELF ']. "? Current_page = ". $ I."> [". $ I."] </a> "; } // Generate a navigation link If ($ this-> current_page> 1 ){ Echo "<A href =". $ _ SERVER ['php _ SELF ']. "? Current_page = ". $ this-> first."> homepage </A> | "; Echo "<A href =". $ _ SERVER ['php _ SELF ']. "? Current_page = ". $ this-> prev."> previous page </A> | "; } If ($ this-> current_page <$ this-> total_pages ){ Echo "<A href =". $ _ SERVER ['php _ SELF ']. "? Current_page = ". $ this-> next."> next page </A> | "; Echo "<A href =". $ _ SERVER ['php _ SELF ']. "? Current_page = ". $ this-> last."> last page </A> "; } Echo "</form> "; Echo "</div> "; } } ?> Call: <? Php Include_once ("../config_jj/sys_conf.inc "); Include_once ("../PageSupportClass. php"); // paging class Include_once ('../smarty_jsnhclass.php '); $ Smarty = new Smarty_Jsnh (); Include_once ("../include/Smarty_changed_dir.php "); $ Smarty-> assign ('title', "Smarty news paging test "); <? Php $ PageSupport = new PageSupport ($ PAGE_SIZE); // instantiate the PageSupport object $ Current_page = $ _ GET ["current_page"]; // the current page number. If (isset ($ current_page )){ $ PageSupport->__ set ("current_page", $ current_page ); } Else { $ PageSupport->__ set ("current_page", 1 ); } ?> $ PageSupport->__ set ("SQL", "select * from news "); $ PageSupport-> read_data (); // read data If ($ pageSupport-> current_records> 0) // if the data is not empty, assemble the data { For ($ I = 0; $ I <$ pageSupport-> current_records; $ I ++) { $ Title = $ pageSupport-> result [$ I] ["title"]; $ Id = $ pageSupport-> result [$ I] ["id"]; $ News_arr [$ I] = array ('News' => array ('id' => $ id, 'title' => $ title )); } } // Close the database Mysql_close ($ db ); $ Pageinfo_arr = array ( 'Total _ records '=> $ pageSupport-> total_records, 'Current _ page' => $ pageSupport-> current_page, 'Total _ pages' => $ pageSupport-> total_pages, 'First' => $ pageSupport-> first, 'Prev' => $ pageSupport-> prev, 'Next' => $ pageSupport-> next, 'Last' => $ pageSupport-> last ); $ Smarty-> assign ('results', $ news_arr ); $ Smarty-> assign ('pagesupport ', $ pageinfo_arr ); $ Smarty-> display ('news/list. tpl '); ?> Template list. tpl {* I am a Smarty comment, I don't exist in the compiled output *} {* {$ PageSupport. total_records} <br/> {$ PageSupport. current_page} <br/> {$ PageSupport. total_pages} <br/> {$ PageSupport. first} <br/> {$ PageSupport. prev} <br/> {$ PageSupport. next} <br/> {$ PageSupport. last} <br/> *} <Html> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = gbk"/> <Title >{$ title }</title> </Head> <Body> {Foreach item = o from = $ results} {$ O. news. id} {$ o. news. title} <Br> {Foreachelse} No data you want to view! {/Foreach} <Br/> {If ($ pageSupport. total_records> 0 )}
<Form action = "" method = "get"> {$ PageSupport. total_records} records Page {$ pageSupport. current_page}/total page {$ pageSupport. total_pages} {If ($ pageSupport. current_page> 1 )} <A href =? Current_page = {$ pageSupport. first}> homepage </A> <A href =? Current_page = {$ pageSupport. prev}> previous page </A> {/If} {If ($ pageSupport. current_page <$ pageSupport. total_pages )} <A href =? Current_page = {$ pageSupport. next}> next page </A> <A href =? Current_page = {$ pageSupport. last}> last page </A> {/If} Jump to the <input type = "text" size = "4" name = "current_page" value = "{$ pageSupport. current_page}"/> page <Input type = "submit" value = "GO"/> </Form> {/If} </Body> </Html>
|