Thinkphp搜索时首页分页和搜索页保持条件分页的方法
这篇文章主要介绍了Thinkphp搜索时首页分页和搜索页保持条件分页的方法,以两种不同的方式详细分析了实现搜索时分页情况下保持搜索条件的方法,是非常具有实用价值
本文实例讲述了Thinkphp实现搜索时首页分页和搜索页保持条件分页的方法。分享给大家供大家参考。具体实现方法如下:
在做搜索查询时突然发现在首页用的分页代码在搜索页使用时出现错误,首页分页代码(代码中标注start与end部分为分页代码)
复制代码 代码如下:
public function index(){
$res=D('Info');// 实例化Data数据对象
/**********start************/
import('ORG.Util.Page');// 导入分页类
$count= $res->count();// 查询满足要求的总记录数
$Page = new Page($count,3);// 实例化分页类 传入总记录数(另一个参数为自定义分页条数)
//$Page->rollPage = 3;//默认情况下,页面显示的页数是5 可以修改
$show= $Page->show();// 分页显示输出
// 进行分页数据查询
$list = $res->order('iid desc')->limit($Page->firstRow.','.$Page->listRows)->select();
/**********end************/
$this->assign('list',$list);// 赋值数据集
/*********start*************/
$this->assign('page',$show);// 赋值分页输出
/*********end*************/
$this->display(); // 输出模板
}
搜索代码(代码中start与end之间标注的部分为分页代码,注释标注了分页跳转时保存查询条件),以下两种方法都可以保存条件(不清楚这样是不是写的规范),查询:
复制代码 代码如下:
public function search() {
$res=D('Info');
$name=$_REQUEST['name'];
$sear['name'] = array('like','%'.$name.'%');
/*********start*************/
import('ORG.Util.Page');// 导入分页类
$count=$res->where($sear)->count();//查询数据条数
$Page=new Page($count,2);//实例化分页函数
/*********end*************/
//分页跳转的时候保存查询条件
foreach($sear as $key=>$val) {
$Page->parameter .= "$key=".urlencode($name)."&";//赋值给Page
}
/*********start*************/
$show=$Page->show();//分页显示输出
// 进行分页数据查询
$val=$res->where($sear)->$val=$res->where($sear)->limit($Page->firstRow.','.$Page->listRows)->select();
/*********end*************/
$this->assign('search',$val);
/*********start*************/
$this->assign('page',$show);
/*********end*************/
$this->display();
}
注:
复制代码 代码如下:
foreach($sear as $key=>$val) {
$Page->parameter .= "$key=".urlencode($name)."&";//赋值给Page
}
"$key=".urlencode($name)."&";
第二种:
复制代码 代码如下:
public function search() {
$res=D('Info');
$name=$_REQUEST['name'];
$sear['name'] = array('like','%'.$name.'%');
import('ORG.Util.Page');// 导入分页类
$count=$res->where($sear)->count();//查询数据条数
$Page=new Page($count,2);//实例化分页函数
//分页跳转的时候保证查询条件
foreach($sear as $key=>$val) {
$Page->parameter .= "$key=".urlencode($val[1]).'&';
}
$show=$Page->show();//分页显示输出
// 进行分页数据查询
$val=$res->where($sear)->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('search',$val);
$this->assign('page',$show);
$this->display();
}
使用$val[1]是因为$sear是一个数组,而$val[1]对应的是我要查找的条件,这样就可以保持条件进行分页了.
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
,
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to set up Google Chrome homepage? Google Chrome is the most popular web browser software today. This browser has simple and efficient features that users like. When using browsers, different people have different settings preferences. Some people like to use Google Chrome. The browser is set as the default homepage, and some people like to set the homepage as other search engines, so where should it be set? Next, the editor will bring you a quick method to set up the homepage of Google Chrome. I hope it can be helpful to you. How to quickly set the Google Chrome homepage 1. Open Google Chrome (as shown in the picture). 2. Click the menu button in the upper right corner of the interface (as shown in the picture). 3. Select the "Settings" option (as shown in the picture). 4. In the settings menu, find "Search Engine" (such as

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.
