search
Homephp教程php手册如何简化php模板页面中分页代码的解析

分页|模板|页面

------------------------
------------------------
这篇文章主要是针对“使用模板的情况”写的,
但是这种方法适合于任何的场合,在任何情况下都是一种比较好的解决方案
------------------------
------------------------
在使用模板的时候,会遇到这么一个问题:显示分页信息时操作麻烦,n多个模板都有分页块。
例如:
---共 20 条记录,当前 3/5 页 首页 上一页 下一页 尾页 GO-----
初遇到这个问题的道友,在考虑解决这个问题的时候好像都是在打php的主意,考虑怎么用php来实现,但是不管你是怎么设计都后设计成两种方案
1、用嵌套循环来实现
2、用n多个判断来搞
但是最终还是比较麻烦的,而且解析的时候是用的服务器端的资源。

不妨换个方法用javascript来代替你的php!!!!,这样即可减少php脚本的代码量,还可以把解析分页的工作交给客户端自己来作。不过javascript调试起来可能会比较麻烦。
最重要的是可以简化分页显示时,解析模板遇到的痛苦。
下面用一个支持pear的itx模板工具解析的模板.
其中表示一个块,{recordcount}这种类似的字符串是变量。

----------------list.tpl---------------------
//其它的html代码


 
  
 

   
   
  

//其它的html代码

--------------------page.js------------
//---------------共 20 条记录,当前 3/5 页 首页 上一页 下一页 尾页 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 if(pageNow str = '

'+recordCount+' 条记录,当前 '+pageNow+'/'+pageCount+' 页';
 if(pageNow  str += " 首页 ";
 else
  str += " 首页 ";
 if(pageNow  str += " 上一页 ";
 else
  str += " 上一页 ";
 if(pageNow>=pageCount)
  str += " 下一页 ";
 else
  str += " 下一页 ";
 if(pageNow>=pageCount)
  str += " 尾页 ";
 else
  str += " 尾页 ";
 str += "跳到页";
 str += "
";
 return str;
}
function pagego0(pageGo,pageNow,pageCount,pageStr){
 if(pageGo>=1 && pageGo  window.location = pageStr.replace("_page_", pageGo);
}

----------------------------
另外这种方法即使不用模板,也可以用,也一样是一种好的分页解决方案,只要将{recordcount}这种类似的字符串用变量的值替换就可以了。
----------------------------




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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor