Example of combining php paging code with smarty
- /**
- * Paging class
- */
- Class Pager {
- Var $ total; // The total number of records.
- Var $ pageSize; // number of records displayed on each page
- Var $ currentPage; // The current page number.
- Var $ offset; // record offset
- Var $ pageTotal; // The total number of pages.
- Var $ numberOffset = 5; // page offset
- Var $ request = ""; // page request parameters
- // ==============================
- // Fn: Pager
- // Function: constructor
- // ==============================
- Public function _ construct ($ total, $ pageSize, $ currentPage, $ request = ""){
- $ This-> total = $ total;
- $ This-> pageSize = $ pageSize;
- $ This-> pageOffset ();
- $ This-> pageTotal ();
- $ This-> currentPage ($ currentPage );
- $ This-> request = $ request;
- }
- // ==============================
- // Fn: pageOffset
- // Function: database record offset
- // ==============================
- Public function pageOffset (){
- Return $ this-> offset = $ this-> pageSize * ($ this-> currentPage-1 );
- }
- // ==============================
- // Fn: pageTotal
- // Function: calculate the total number of pages
- // ==============================
- Public function pageTotal (){
- Return $ this-> pageTotal = ceil ($ this-> total/$ this-> pageSize );
- }
- // ==============================
- // Fn: currentPage
- // Function: set the page number
- // ==============================
- Public function currentPage ($ currentPage ){
- If (isset ($ currentPage )){
- $ This-> currentPage = intval ($ currentPage );
- } Else {
- $ This-> currentPage = 1;
- }
- Return $ this-> currentPage;
- }
- // ==============================
- // Fn: nextPage
- // Function: Jump to the next page
- // ==============================
- Public function nextPage (){
- // Display the number of records
- $ Link = "total {$ this-> total} items ";
- // Page number step size
- $ StepPage = $ this-> currentPage? Ceil ($ this-> currentPage/$ this-> numberOffset): 1;
- // Set the number page number
- $ NumberPage = ($ this-> pageTotal> $ this-> numberOffset )? $ This-> numberOffset: $ this-> pageTotal;
- // Only one page
- If ($ this-> total <= $ this-> pageSize ){
- $ Link. = "[Homepage] | [last page]";
- } Else {
- // Set the total number of pages and the current page
- $ Link. = "page {$ this-> currentPage}/{$ this-> pageTotal ";
- // Homepage
- $ Link. = "request}> [Homepage]";
- // Next column
- If ($ stepPage> 1 ){
- $ LastIndex = ($ stepPage-1) * $ this-> numberOffset;
- $ Link. = "request}> [<]";
- }
- // Previous Page
- If ($ this-> currentPage> 1 ){
- $ PrePage = $ this-> currentPage-1;
- $ Link. = "request}> [<]";
- }
- // Number page
- $ I = ($ stepPage-1) * $ this-> numberOffset;
- For ($ j = $ I; $ j <($ I + $ numberPage) & $ j <$ this-> pageTotal; $ j ++ ){
- $ NewPage = $ j + 1;
- If ($ this-> currentPage ==$ j + 1 ){
- $ Link. ="[". ($ J + 1)."]";
- } Else {
- $ Link. = "request}> [". ($ j + 1). "]";
- }
- }
- // Next page
- If ($ this-> currentPage <$ this-> pageTotal ){
- $ NextPage = $ this-> currentPage + 1;
- $ Link. = "request}> [>]";
- }
- // Next column
- If ($ stepPage <$ this-> total ){
- $ NextPage = $ stepPage * ($ this-> numberOffset + 1 );
- If ($ nextPage <$ this-> pageTotal ){
- $ Link. = "request}> [>]";
- }
- }
- // Last Page
- If ($ this-> currentPage <$ this-> pageTotal ){
- $ Link. = "... pageTotal }{$ this-> request}> [last page]";
- }
- }
- Return $ link;
- }
- }
- ?>
II. php paging call example:
1. get the page pages returned from the URL:
- $ Cur_page = 1;
- If (isset ($ _ GET ["pageNo"]) {
- $ Cur_page = $ _ GET ["pageNo"];
- }
2. create a paging object:
- $ Nums: total number of data records
- $ Page_size: Number of Entries displayed per page
- $ Cur_page: current page number
- $ Request: optional parameters for other Url requests
- $ Pager = new Pager ($ nums, $ page_size, $ cur_page, $ request );
3. smarty value assignment:
- $ Show = get the data to be displayed
- $ This-> tpl-> assign ('numlink', $ pager-> nextPage (); // Obtain the page List
- $ This-> tpl-> assign ('data', $ show );
Paging effect:
The above paging code does not implement url redirection, so that all transmitted information in the address bar is exposed, and you can complete it. |