search
Homephp教程php手册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)."&";

这里面的$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
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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor