PHP头条
热点:

Thinkphp分页代码(首页分页和搜索页保持条件分页)


在做搜索查询时突然发现在首页用的分页代码在搜索页使用时出现错误,首页分页代码(绿色为分页代码)

  public function index(){   $res=D('Info');// 实例化Data数据对象   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();   $this->assign('list',$list);// 赋值数据集   $this->assign('page',$show);// 赋值分页输出   $this->display(); // 输出模板     }

搜索代码(绿色为分页代码,其中的红色为保存条件),以下两种方法都可以保存条件(不清楚这样是不是写的规范),查询:

  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($name)."&";//赋值给Page    }    $show=$Page->show();//分页显示输出    // 进行分页数据查询     $val=$res->where($sear)->$val=$res->where($sear)->limit($Page->firstRow.','.$Page->listRows)->select();    $this->assign('search',$val);    $this->assign('page',$show);    $this->display();       }

注:

   foreach($sear as $key=>$val) {     $Page->parameter   .=   "$key=".urlencode($name)."&";//赋值给Page    }    "$key=".urlencode($name)."&";这里面的$name 相对应提取的是 $name=$_REQUEST['name'];获取的值。 

第二种:

  1. public function search() { 
  2.    $res=D('Info'); 
  3.    $name=$_REQUEST['name']; 
  4.    $sear['name'] = array('like','%'.$name.'%'); 
  5.    import('ORG.Util.Page');// 导入分页类 
  6.    $count=$res->where($sear)->count();//查询数据条数 
  7.    $Page=new Page($count,2);//实例化分页函数 
  8.    //分页跳转的时候保证查询条件 
  9.    foreach($sear as $key=>$val) {     
  10.     $Page->parameter   .=   "$key=".urlencode($val[1]).'&'
  11.    } 
  12.    $show=$Page->show();//分页显示输出 
  13.    // 进行分页数据查询 
  14.    $val=$res->where($sear)->limit($Page->firstRow.','.$Page->listRows)->select(); 
  15.    $this->assign('search',$val); 
  16.    $this->assign('page',$show); 
  17.    $this->display(); 
  18.     
  19.   } 

使用$val[1]是因为$sear是一个数组,而$val[1]对应的是我要查找的条件,这样就可以保持条件进行分页了.

www.phpzy.comtrue/phpkj/7400.htmlTechArticleThinkphp分页代码(首页分页和搜索页保持条件分页) 在做搜索查询时突然发现在首页用的分页代码在搜索页使用时出现错误,首页分页代码(绿色为分页代码) public function index(){ $res=D(Inf...

相关文章

相关频道:

PHP之友评论

今天推荐