Home  >  Article  >  Backend Development  >  Simplify the parsing of pagination code in PHP template pages_PHP tutorial

Simplify the parsing of pagination code in PHP template pages_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:48:12859browse

When using templates, you will encounter such a problem: it is troublesome to display paging information, and multiple templates have paging blocks.
For example:
---20 records in total, current 3/5 pages Home Page Previous Page Next Page Last Page GO-----
Friends who have encountered this problem for the first time are considering When solving this problem, it seems that everyone is thinking about how to use PHP to implement it, but no matter how you design it, there are two options: 1. Use nested loops to implement it
2. It uses n multiple judgments to do
but it is still troublesome in the end, and server-side resources are used during parsing.
Why not use javascript instead of php! ! ! ! , this can reduce the amount of code in the php script, and can also leave the work of parsing paging to the client itself. However, debugging JavaScript may be more troublesome.
The most important thing is that it can simplify the pain of parsing templates when displaying pagination.
The following is a template parsed by an itx template tool that supports pear.
Where and represent a block, {recordcount} Such similar strings are variables.
----------------list.tpl--------------------------

Copy code The code is as follows:
//Other html code







//Other html code
--------------------page.js------ ------
//--------------- 20 records in total, current 3/5 pages Home Page Previous Page Next Page Last Page GO---- ---------------
//recordCount = 20;
//show = 20
//pageCount = 5;
//pageNow = 3;
//pageStr = "?page=_page_";
//document.write(showListPage(recordCount, show, pageCount, pageNow, pageStr));
function showListPage0(recordCount, show, pageCount, pageNow , pageStr){
if(pageCount<1) pageCount =0;
if(pageNow<1) pageNow = 0;
str = '
total records, current'+pageNow+'/'+pageCount+' page';
if(pageNow<=1)
str + = "Home";
else
str += " Homepage ";
if(pageNow<=1)
str += " Previous page";
else
str += " < A href=""+pageStr.replace("_page_",(pageNow-1))+"" href=""+pageStr.replace("_page_",(pageNow-1))+"">Previous page ";
if(pageNow>=pageCount)
str += " Next page";
else
str += " Next page ";
if(pageNow>=pageCount)
str += " Last page";
else
str += " Last page ";
str += "Jump topage";
str += "";
return str;
}
function pagego0(pageGo,pageNow,pageCount,pageStr){
if(pageGo>=1 && pageGo<=pageCount && pageNow!=pageGo)
window.location = pageStr.replace("_page_", pageGo);
}

Also This method can be used even without templates, and it is still a good paging solution. Just replace strings like {recordcount} with the value of a variable.

http://www.bkjia.com/PHPjc/319798.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319798.htmlTechArticleWhen using templates, you will encounter such a problem: it is troublesome to display pagination information, n multiple templates All have paging blocks. For example: ---20 records in total, current page 3/5 Home Previous...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn