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

简化php模板页面中分页代码的解析

Jun 13, 2016 pm 12:25 PM
phpcodeusePaginationexistshowtemplateofsimplifyparsemeetquestionpage

在使用模板的时候,会遇到这么一个问题:显示分页信息时操作麻烦,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(pageCountif(pageNowstr = '
'+recordCount+' 条记录,当前 '+pageNow+'/'+pageCount+' 页';
if(pageNowstr += " 首页 ";
else
str += " 首页 ";
if(pageNowstr += " 上一页 ";
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 && pageGowindow.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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool