Join the PHP paging class of the smarty template and database operation class, pass in SQL statements, and pass out the smarty Array

Source: Internet
Author: User
Tags smarty template
<? PHP
/***
* By fallen moon shadow <aofanliguo@126.com>
* Please mail me when you find errors!
*/
// Contains common database operations
// Include 'db. Class. php ';
// Contains the smarty database operation class library
// Include "Smarty/libs/smarty. Class. php ";
// Of course, it can also be included in the parent directory
//-----------------------------------------------------------
Class pagesmarty {
// Class Input
VaR $ db; // The parameter is passed in for database operations
VaR $ TPL; // The template technology used for parameter input

VaR $ SQL; // query data operations

// Paging related
VaR $ URL;
VaR $ pagesize = 20; // you can set
VaR $ totalnumber =-1;
VaR $ page_number =-1;
VaR $ pager_pageid =-1;
VaR $ pager_links = "";
/***-------------------------------------
*/
Function pagesmarty ($ db, $ TPL, $ SQL, $ url = ""){
// We recommend that you pass URL variables to better control leaf circulation and improve portability.
// Corresponding smarty settings
// There is a relative path problem here. When loading the include statement, it is relative to this directory
// When the directory is contained by the upper-level directory, the following path setting problems are specific to the upper-level directory.
// When there is a path problem, you can use a format similar to $ page-> TPL-> template to overwrite
$ This-> TPL = $ TPL;

$ This-> TPL-> template_dir = "libs/templates /";
$ This-> TPL-> compile_dir = "libs/templates_c /";
$ This-> TPL-> config_dir = "libs/configs /";
$ This-> TPL-> cache_dir = "libs/Cache /";
$ This-> TPL-> left_delimiter = '<{';
$ This-> TPL-> right_delimiter = '}> ';

$ This-> SQL = $ SQL;
$ This-> DB = $ dB;
$ This-> url = $ URL;
}
/***------------------------------------
*/
// Set the current number per page. The default value is 20.
Function setpagesize ($ pagesize ){
$ This-> pagesize = $ pagesize;
}
/***------------------------------------
*/
// Obtain the total number of records
Function gettotalnumber (){
$ This-> DB-> query_withresult ($ this-> SQL );
$ This-> totalnumber = $ this-> DB-> db_num_rows ();
Return $ this-> totalnumber;
}
/***------------------------------------
*/
// Obtain the total number of pages
Function getpagenumber (){
$ This-> page_number = Ceil ($ this-> totalnumber/$ this-> pagesize );
Return $ this-> page_number;
}
/***------------------------------------
*/
// Save the result set to an array available for smarty
// Pay attention to the selected range
Function makelist (){
If ($ this-> totalnumber <= $ this-> pagesize ){
// If the number of data records is less than the number of leaves, use this mode to display all
For ($ I = 0; $ I <= $ this-> totalnumber; $ I ++ ){
$ This-> DB-> ROW = mysql_fetch_array ($ this-> DB-> result );
$ Data [] = $ this-> DB-> row;
}
// Normal Mode for forward order
} Else {
For ($ I = ($ this-> pager_PageID-1) * $ this-> pagesize; $ I <($ this-> pager_pageid) * $ this-> pagesize; $ I ++ ){
// If the data display exceeds the total number range --> stop! The last page
If ($ I >=$ this-> totalnumber) {break ;}
$ This-> DB-> db_data_seek ($ this-> DB-> result, $ I );
$ This-> DB-> ROW = mysql_fetch_array ($ this-> DB-> result );
// Use smarty to register the obtained data to the display area for processing.
$ Data [] = $ this-> DB-> row;
}
}
Return $ data;
}
/***------------------------------------
*/
// Obtain the current page number from the get Parameter
Function getcurpage (){
If (isset ($ _ Get ['pager _ pageid']) &! Empty ($ _ Get ['pager _ pageid']) {
$ This-> pager_pageid = intval ($ _ Get ['pager _ pageid']);
} Else {
// If this is the first access, set the current page to the first page
$ This-> pager_pageid = 1;
}
Return $ this-> pager_pageid;
}
/***------------------------------------
*/
// Create a list of pages to be displayed
Function makepagelist (){
// Prevent error submission
If ($ this-> pager_pageid <= 0) $ this-> pager_pageid = 1;
If ($ this-> pager_pageid >=$ this-> page_number) $ this-> pager_pageid = $ this-> page_number;
// For the first page
If ("1" = $ this-> pager_pageid ){
If ($ this-> page_number> 1 ){
$ This-> pager_links = "<a href =". $ this-> URL ."? Pager_pageid = 2> next page </a> & nbsp; <a href = ". $ this-> URL ."? Pager_pageid = ". $ this-> page_number."> last page </a> ";
} Else {
$ This-> pager_links = "last page ";
}
}
// For The Last Page
Elseif ($ this-> page_number = $ this-> pager_pageid ){
$ This-> pager_links = "<a href =". $ this-> URL ."? Pager_pageid = 1> homepage </a> & nbsp; <a href = ". $ this-> URL ."? Pager_pageid = ". ($ this-> pager_PageID-1)."> previous page </a> ";
}
// General intermediate condition
Else {
$ This-> pager_links = "<a href =". $ this-> URL ."? Pager_pageid = ". ($ this-> pager_PageID-1)."> previous page </a> & nbsp; <a href = ". $ this-> URL ."? Pager_pageid = ". ($ this-> pager_pageid + 1)."> next page </a> ";
}
Return $ this-> pager_links;
}
/***------------------------------------
*/
// Execute the corresponding operation to pass some variables that may be used. The specific variable transmission can be used at the control layer.
Function exec ($ pageto ){
$ This-> TPL-> assign ("curpage", $ this-> getcurpage (); // the current page number
$ This-> TPL-> assign ("totalnumber", $ this-> gettotalnumber (); // The total number of records
$ This-> TPL-> assign ("pagenumber", $ this-> getpagenumber (); // the total number of pages
$ This-> TPL-> assign ("pagelinks", $ this-> makepagelist (); // page number navigation
$ This-> TPL-> assign ("list", $ this-> makelist (); // specific data
$ This-> TPL-> display ($ pageto); // call the corresponding leaf area of the template
Return $ this;
}
/***-----------------------------------
/* Example ////////////////////////////
Include 'config. php ';
Require "libs/page. Smarty. Class. php ";
$ Db = new dB (host, db_user, db_password, db_database );
$ Conn = $ db-> conn ();
$ TPL = new smarty ();
// You can set transmission parameters at the control layer.
$ TPL-> assign ("title", "this is lycy ");
$ TPL-> assign ("content", "I want to display lycy ");
///////////////////////////////////////
$ SQL = "select * From guest_main ";
$ Url = "test. php ";
$ Page = new pagesmarty ($ db, $ TPL, $ SQL, $ URL );
$ Page-> setpagesize (5 );
// Construct a database paging class and join with smarty. Four parameters need to be input.
// Transfer template foliar
$ Page-> exec ("test.html ");
****************************************
// Template page
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> <{$ title}> </title>
</Head>
<Body>
Somecontent = <{$ content}>
<Br>
Totalpage = <{$ totalnumber}>
<Br>
Pagenumber = <{$ pagenumber}>
<Br>
Curpage = <{$ curpage}>
<Br>
<{$ Pagelinks}>
<Br>
<{Section name = n loop = $ list}>
<{$ List [N]. guest_title}>
<Br>
<{/Section}>
</Body>
</Html>
------------------------------------*/
}
?>

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.