Home Backend Development PHP Tutorial Thinkphp搜索时首页分页和搜索页保持条件分页的方法_PHP

Thinkphp搜索时首页分页和搜索页保持条件分页的方法_PHP

May 31, 2016 pm 06:15 PM
thinkphp Pagination search front page

本文实例讲述了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)."&";

这里面的$name 相对应提取的是 $name=$_REQUEST['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程序设计有所帮助。

Statement of this Website
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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

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.

How to search for users in Xianyu How to search for users in Xianyu Feb 24, 2024 am 11:25 AM

How does Xianyu search for users? In the software Xianyu, we can directly find the users we want to communicate with in the software. But I don’t know how to search for users. Just view it among the users after searching. Next is the introduction that the editor brings to users about how to search for users. If you are interested, come and take a look! How to search for users in Xianyu? Answer: View details among the searched users. Introduction: 1. Enter the software and click on the search box. 2. Enter the user name and click Search. 3. Select [User] under the search box to find the corresponding user.

How to use Baidu advanced search How to use Baidu advanced search Feb 22, 2024 am 11:09 AM

How to use Baidu Advanced Search Baidu search engine is currently one of the most commonly used search engines in China. It provides a wealth of search functions, one of which is advanced search. Advanced search can help users search for the information they need more accurately and improve search efficiency. So, how to use Baidu advanced search? The first step is to open the Baidu search engine homepage. First, we need to open Baidu’s official website, which is www.baidu.com. This is the entrance to Baidu search. In the second step, click the Advanced Search button. On the right side of the Baidu search box, there is

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

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 How to set up Google Chrome homepage Mar 02, 2024 pm 04:04 PM

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

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

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.

WPS table cannot find the data you are searching for, please check the search option location WPS table cannot find the data you are searching for, please check the search option location Mar 19, 2024 pm 10:13 PM

In the era dominated by intelligence, office software has also become popular, and Wps forms are adopted by the majority of office workers due to their flexibility. At work, we are required not only to learn simple form making and text entry, but also to master more operational skills in order to complete the tasks in actual work. Reports with data and using forms are more convenient, clear and accurate. The lesson we bring to you today is: The WPS table cannot find the data you are searching for. Why please check the search option location? 1. First select the Excel table and double-click to open it. Then in this interface, select all cells. 2. Then in this interface, click the "Edit" option in "File" in the top toolbar. 3. Secondly, in this interface, click "

How to search for stores on mobile Taobao How to search for store names How to search for stores on mobile Taobao How to search for store names Mar 13, 2024 am 11:00 AM

The mobile Taobao app software provides a lot of good products. You can buy them anytime and anywhere, and everything is genuine. The price tag of each product is clear. There are no complicated operations at all, making you enjoy more convenient shopping. . You can search and purchase freely as you like. The product sections of different categories are all open. Add your personal delivery address and contact number to facilitate the courier company to contact you, and check the latest logistics trends in real time. Then some new users are using it for the first time. If you don’t know how to search for products, of course you only need to enter keywords in the search bar to find all the product results. You can’t stop shopping freely. Now the editor will provide detailed online methods for mobile Taobao users to search for store names. 1. First open the Taobao app on your mobile phone,

See all articles