ThinkPHP paging function example details, thinkphp paging details
This example describes the thinkPHP paging function. We will share this with you for your reference. The details are as follows:
Interface ServiceInterFace:
<? Php/*** InterFaceService * @ author yhd */namespace Red; interface ServiceInterFace {/*** instantiate the current class */public static function getInstance ();}
StaticService static service class:
<? Php/*** static service class * StaticService * @ author yhd */namespace Red; class StaticService {protected static $ data; /*** set static data ** @ param string $ key * @ param mixed $ data * @ return mixed */public static function setData ($ key, $ data) {self: $ data [$ key] = $ data; return self: $ data [$ key];} /*** use static data by referencing * @ param string $ key * @ return mixed */public static function & getData ($ key) {if (! Isset (self ::$ data [$ key]) {self ::$ data [$ key] = null;} return self ::$ data [$ key];} /*** cached instantiated object * @ param string $ name class name * @ return object */public static function getInstance ($ name) {$ key = 'service _@_'. $ name; $ model = & self: getData ($ key); if ($ model = null) {$ model = new $ name ();} return $ model;}/*** html Escape filter * @ param mixed $ input * @ return mixed */public static function htmlFilter ($ inpu T) {if (is_array ($ input) {foreach ($ input as & $ row) {$ row = self: htmlFilter ($ row );}} else {if (! Get_magic_quotes_gpc () {$ input = addslashes ($ input) ;}$ input = htmlspecialchars ($ input);} return $ input ;}}
Abstract AbProduct abstract product management class:
<? Php/*** abstract product management class ** AbProduct. class. php * @ lastmodify 2015-8-17 * @ author yhd */namespace Red \ Product; abstract class AbProduct {public $ errorNum; /** return error message * @ param $ errorNum error code */public function GetStatus () {$ errorNum = $ this-> errorNum; switch ($ errorNum) {case 0: $ data ['status'] = 0; $ data ['message'] = 'favorites successfully'; break; case 1: $ data ['status'] = 1; $ data ['message'] = 'favorites failed'; break; case 2: $ data ['status'] = 2; $ data ['message'] = 'added to favorites '; break; case 3: $ data ['status'] = 3; $ data ['message'] = 'unlogged in '; break; case 4: $ data ['status'] = 4; $ data ['message'] = 'parameter missing '; break; default: $ data ['status'] = 200; $ data ['message'] = 'unknown error';} return $ data ;}
MemberModel member model:
<? Php/*** member model * MemberModel. class. php * @ copyright (C) 2014-2015 red * @ license http://www.red.com/* @ lastmodify 2015-8-17 * @ author yhd */namespace Red \ Passport \ Models; use Think \ Model; use Red \ ServiceInterFace; use Red \ StaticService; class MemberModel extends Model implements ServiceInterFace {protected $ userId; protected $ error; protected function _ initialize () {$ this-> userId = getUserInfo (0);}/*** instantiate This CLASS * @ return MemberModel */public static function getInstance () {return StaticService: getInstance (_ CLASS __);} /*** get Login User information * @ param string $ data Query condition * @ return array */public function getUser ($ data = '') {if (empty ($ data) {return $ this-> where ("id = ". $ this-> userId)-> find ();} else {return $ this-> field ($ data)-> where ("id = ". $ this-> userId)-> find () ;}/ *** modify user information * @ param array $ data * @ param Array $ where query condition */public function editUserInfo ($ data, $ where = '') {if ($ this-> _ before_check ($ data) = false) {return $ this-> error ['msg '];} if (! Empty ($ where) & is_array ($ where) {$ condition [$ where [0] = array ('eq ', $ where [1]); return $ this-> where ($ condition)-> save ($ data);} return $ this-> where ("id = ". $ this-> userId)-> save ($ data);}/*** get user information * @ param string $ data username * return array () */public function checkUserInfo ($ str, $ field = '') {// registration type $ info = CheckType ($ str ); $ condition [$ info] = array ('eq ', $ str); if (! Empty ($ field) {return $ this-> field ($ field)-> where ($ condition)-> find ();} return $ this-> where ($ condition)-> find ();}/*** get user information * @ param array $ data username * return array () */public function getAccount ($ data) {// registration type $ info = CheckType ($ data); $ condition ['id'] = array ('eq ', $ this-> userId); $ condition [$ info] = array ('eq ', $ data); return $ this-> where ($ condition) -> find ();} /*** Change User passWord ** @ param array $ data ['id'] user id * @ param $ data ['Password'] user passWord * return true or false */public function upUserPassById ($ data) {$ condition ['id'] = array ('eq ', $ data ['id']); $ status = $ this-> where ($ condition) -> save (array ("password" => md5 ($ data ['Password']); if ($ status) {return TRUE ;} else {return FALSE ;}} /*** check whether the user's account or password is correct * @ param $ data ['username'] user name * @ param $ data ['Password'] password * return true or false * /public function checkUserPasswd ($ data = array ()) {$ type = CheckType ($ data ['username']); $ condition [$ type] = array ('eq ', $ data ['username']); $ condition ['Password'] = array ('eq ', md5 ($ data ['Password']); return $ this-> where ($ condition) -> find ();}/*** token for webpage logon verification * @ param token string * return bool */public function checkToken ($ token) {return $ this-> autoCheckToken ($ token);}/*** background seal/unseal * param int $ user_id */public function changeStatus ($ data) {if ($ this-> save ($ data) {return true;} else {return false;} protected function _ before_check (& $ data) {if (isset ($ data ['username']) & empty ($ data ['username']) {$ this-> error ['msg '] = 'Enter the Username'; return false;} if (isset ($ data ['nickname']) & empty ($ data ['nickname']) {$ this-> error ['msg '] = 'Enter the nickname'; return false ;} if (isset ($ data ['realname']) & empty ($ data ['realname']) {$ this-> error ['msg '] = 'enter your real name'; return false;} if (isset ($ data ['email ']) & empty ($ data ['email ']) {$ this-> error ['msg'] = 'enter your mailbox '; return false ;} if (isset ($ data ['mobile']) & empty ($ data ['mobile']) {$ this-> error ['msg '] = 'Enter the mobile phone number'; return false;} if (isset ($ data ['Password']) & empty ($ data ['Password']) {$ this-> error ['msg '] = 'enter password'; return false ;} if (isset ($ data ['uploadmg ']) & empty ($ data ['uploadmg']) {$ this-> error ['msg '] = 'upload Avatar'; return false;} return true ;}}
ProductModel Product Model:
<? Php/*** Product Model * ProductModel. class. php * @ lastmodify 2015-8-17 * @ author yhd */namespace Red \ Product \ Models; use Think \ Model; use Red \ ServiceInterFace; use Red \ StaticService; class ProductModel extends Model implements ServiceInterFace {/*** instantiate this class * @ return ProductModel */public static function getInstance () {return StaticService :: getInstance (_ CLASS _);}/*** single item * @ param string $ id * @ param integer $ Status 1: Valid 2: Invalid * @ param integer $ onsale mounting? 1: Yes 2: No * @ return array one-dimensional array */public function getProOne ($ id, $ status = 1, $ onsale = 1) {$ condition ['onsale'] = array ('eq ', $ onsale ); // whether to mount $ condition ['status'] = array ('eq ', $ status); // status $ condition ['id'] = array ('eq ', $ id); return $ this-> where ($ condition)-> find ();} /*** item list ** @ param string $ Number of limit queries * @ param array $ data Query condition * @ return ar Ray two-dimensional array */public function getProList ($ data = '') {$ condition ['onsale'] = array ('eq ', $ data ['onsale']); // whether to mount $ condition ['status'] = array ('eq ', $ data ['status']); // Status $ condition ['type'] = array ('eq ', $ data ['type']); // category if (isset ($ data ['limit']) & isset ($ data ['order']) {$ return = $ this-> where ($ condition)-> limit ($ data ['limit'])-> order ($ data ['order']) -> select ();} else {$ return = $ this-> where ($ co Ndition)-> select ();} return $ return;}/*** add product * @ param array $ data * @ return int */public function addProduct ($ data) {return $ this-> add ($ data);}/*** delete item **/public function delProduct ($ id) {$ condition ['id'] = array ('eq ', $ id); return $ this-> where ($ condition)-> delete ();} /*** modify product * @ param string | int $ id * @ param array $ data * @ return */public function editProdcut ($ id, $ data) {$ condit Ion ['id'] = array ('eq ', $ id); return $ this-> where ($ condition)-> save ($ data );} public function getProductInfo ($ product) {if (empty ($ product) |! Isset ($ product ['product _ id']) {return array () ;}$ info = $ this-> getProOne ($ product ['product _ id']); $ product ['name'] = $ info ['name']; $ product ['store _ id'] = $ info ['store _ id']; $ product ['price'] = $ info ['price']; $ product ['m _ price'] = $ info ['m _ price']; return $ product ;}}
ProductManage product management:
<?php namespace User\Controller; use Red\Product\ProductManage; class FavoriteController extends AuthController { public function index($page=1){ $limit=1; $list = ProductManage::getInstance()->getCollectList($page,$limit); $showpage = create_pager_html($list['total'],$page,$limit); $this->assign(get_defined_vars()); $this->display(); } public function cancelCollect(){ $ids = field('ids'); $return = ProductManage::getInstance()->cancelProductCollect($ids); exit(json_encode($return)); }}
Functions. php paging functions:
<? Php/*** page ** @ param $ total number of items * @ param $ page * @ param $ number of items per page * @ param $ url link address * @ param $ maxpage Max page number * @ return string Max page number */function create_pager_html ($ total, $ page = 1, $ perpage = 20, $ url = '', $ maxpage = null) {$ totalcount = $ total; if (empty ($ url) |! Is_string ($ url) {$ url = array (); foreach ($ _ GET as $ k => $ v) {if ($ k! = 'Page') {$ url [] = urlencode ($ k ). '= '. urlencode ($ v) ;}}$ url [] = 'page = {page} '; $ url = '? '. Implode ('&', $ url);} if ($ total <= $ perpage) return ''; $ total = ceil ($ total/$ perpage ); $ pagecount = $ total; $ total = ($ maxpage & $ total> $ maxpage )? $ Maxpage: $ total; $ page = intval ($ page); if ($ page <1 | $ page> $ total) $ page = 1; $ pages = '<div class = "pages"> <a href = "'. str_replace ('{page}', $ page-1 <= 0? 1: $ page-1, $ url ). '"rel =" external nofollow "title =" Previous Page "class =" page_start "> previous page </a> '; if ($ page> 4 & $ page <= $ total-4) {$ mini = $ page-3; $ maxi = $ page + 2 ;} elseif ($ page <= 4) {$ mini = 2; $ maxi = $ total-2 <7? $ Total-2: 7;} elseif ($ page> $ total-4) {$ mini = $ total-7 <3? 2: $ total-7; $ maxi = $ total-2;} for ($ I = 1; $ I <= $ total; $ I ++) {if ($ I! = $ Page) {$ pages. = '<a class = "page-num" href = "'. str_replace ('{page}', $ I, $ url ). '"rel =" external nofollow "> '. $ I. '</a>';} else {$ pages. = '<span class = "page_cur"> '. $ I. '</span>';} if ($ maxi & $ I >=$ maxi) {$ I = $ total-2; $ maxi = 0 ;} if ($ I = 2 or $ total-2 = $ I) & $ total> 10) {$ pages. = '';} if ($ mini & $ I >=2) {$ I = $ mini; $ mini = 0 ;}} $ pages. = '<a href = "'. str _ Replace ('{page}', $ page + 1 >=$ total? $ Total: $ page + 1, $ url ). '"rel =" external nofollow "title =" next page "class =" page_next "> next page </a> <span class =" pageOp "> <span class =" sum "> total '. $ totalcount. 'entries </span> <input type = "text" class = "pages_indium" id = "pageno" value = "'. $ page. '"onkeydown =" if (event. keyCode = 13 & this. value) {window. location. href = \''. $ url. '\'. replace (/\ {page \}/, this. value); return false;} "> <span class =" page-sum "> /'. $ total. 'page </span> <input type = "button" class = "pages_btn" value = "GO" onclick = "if (document. getElementById (\ 'pageno \'). value> 0) window. location. href = \''. $ url. '\'. replace (/\ {page \}/, document. getElementById (\ 'pageno \'). value); "> </span> </div> '; return $ pages ;}