Simplify the parsing of paging code on the PHP template page

Source: Internet
Author: User

When using a template, you may encounter the following problem: the operation is troublesome when the page information is displayed. N templates have paging blocks.
For example:
--- A total of 20 records. Go -----
When I first encountered this problem, it seemed that I was playing the PHP idea to solve it. How can I use PHP to implement it, however, no matter how you design it, design it into two solutions.
1. Implement with nested loops
2. Using n multiple judgments
But in the end, it is quite troublesome, and the server-side resources are used for parsing.
Use JavaScript to replace your PHP !!!!, This reduces the number of PHP scripts Code You can also hand over the resolution page to the client. However, JavaScript debugging may be troublesome.
The most important thing is to simplify the difficulties encountered by parsing templates during paging display.
The following is a template that supports parsing by Pear itx template tool.
<! -- Begin page --> and <! -- End page --> indicates a block. A string similar to {recordcount} is a variable.
---------------- List. TPL --------------------- Copy code The Code is as follows: // other HTML code
<Table border = "0" cellpadding = "0" cellspacing = "0" width = "100%">
<Tr>
<TD align = "right" nowrap>
<SCRIPT src = "images/page. js" src = "images/page. js" Language = "JavaScript"> </SCRIPT>
<Script language = "JavaScript"> <! --
Begin page -->
Recordcount = {recordcount };
Show = {showinonepage}
Pagecount = {pagecount };
Pagenow = {page };
Pagestr = "? Page = _ page _";
Document. Write (showlistpage0 (recordcount, show, pagecount, pagenow, pagestr ));
<! -- End page
// --> </SCRIPT>
</TD>
</Tr>
</Table>
// Other HTML code
------------------ Page. js ------------
// --------------- A total of 20 records. 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 = '<form name = "frmpage"> total <B>' + recordcount + '</B> records, the current <B> '+ pagenow +'/'+ pagecount +' </B> page ';
If (pagenow <= 1)
STR + = "Homepage ";
Else
STR + = "<a href =" "+ pagestr. replace ("_ page _", 1) + "" href = "" + pagestr. replace ("_ page _", 1) + ""> homepage </a> ";
If (pagenow <= 1)
STR + = "Previous Page ";
Else
STR + = "<a href =" "+ pagestr. replace ("_ page _", (pageNow-1) + "" href = "" + pagestr. replace ("_ page _", (pageNow-1) + ""> previous page </a> ";
If (pagenow> = pagecount)
STR + = "next page ";
Else
STR + = "<a href =" "+ pagestr. replace ("_ page _", (pagenow + 1) + "" href = "" + pagestr. replace ("_ page _", (pagenow + 1) + ""> next page </a> ";
If (pagenow> = pagecount)
STR + = "last page ";
Else
STR + = "<a href =" "+ pagestr. replace ("_ page _", pagecount) + "" href = "" + pagestr. replace ("_ page _", pagecount) + ""> last page </a> ";
STR + = "Jump to <input type = \" text \ "name = \" txtpage \ "size = \" 3 \ "> page ";
STR + = "<input type = \" button \ "value = \" Go \ "onclick = \" pagego0(document.frmpage.txt page. value, "+ pagenow +", "+ pagecount +", '"+ pagestr +"') \ "> </form> ";
Return STR;
}
Function pagego0 (pagego, pagenow, pagecount, pagestr ){
If (pagego> = 1 & pagego <= pagecount & pagenow! = Pagego)
Window. Location = pagestr. Replace ("_ page _", pagego );
}

In addition, this method can be used even if no template is used. It is also a good paging solution. You only need to replace the {recordcount} string with the variable value.

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.