刚刚开始学习 Thinkphp,分页其实很简单,但是做查询的时候,点查询结果的分页时,查询条件会丢失,
用下面的方法可以保存查询条件。
/** +---------------------------------------------------------- * 根据表单生成查询条件 * 进行列表过滤 +--------------------------------------------------------- +---------------------------------------------------------- * @param Model $model 数据对象 * @param HashMap $map 过滤条件 * @param string $sortBy 排序 * @param boolean $asc 是否正序 +---------------------------------------------------------- * @return void +---------------------------------------------------------- * @throws ThinkExecption +---------------------------------------------------------- */ function _list($model,$map,$sortBy='',$asc=true) { //排序字段 默认为主键名 if(isset($_REQUEST['order'])) { $order = $_REQUEST['order']; }else { $order = !empty($sortBy)? $sortBy: $model->getPk(); } //排序方式默认按照倒序排列 //接受 sost参数 0 表示倒序 非0都 表示正序 if(isset($_REQUEST['sort'])) { $sort = $_REQUEST['sort']?'asc':'desc'; }else { $sort = $asc?'asc':'desc'; } //取得满足条件的记录数 $count = $model->count($map); if($count>0) { import("ORG.Util.Page"); //创建分页对象 if(!empty($_REQUEST['listRows'])) { $listRows = $_REQUEST['listRows']; }else { $listRows = ''; } $p = new Page($count,$listRows); //分页查询数据 $voList = $model->where($map)->order($order.' '.$sort)->limit($p->firstRow.','.$p->listRows)->findAll(); //分页跳转的时候保证查询条件 foreach($map as $key=>$val) { if(is_array($val)) { foreach ($val as $t){ $p->parameter .= $key.'[]='.urlencode($t)."&"; } }else{ $p->parameter .= "$key=".urlencode($val)."&"; } } //分页显示 $page = $p->show(); //列表排序显示 $sortImg = $sort ; //排序图标 $sortAlt = $sort == 'desc'?'升序排列':'倒序排列'; //排序提示 $sort = $sort == 'desc'? 1:0; //排序方式 //模板赋值显示 $this->assign('list', $voList); $this->assign('sort', $sort); $this->assign('order', $order); $this->assign('sortImg', $sortImg); $this->assign('sortType', $sortAlt); $this->assign("page", $page); } return ; }
还有一个
function search() { import('ORG.Util.HashMap'); $condition = new HashMap(); if($_GET['website'] != '' && $_GET['website'] != 'Seller') { //$condition .=" website like '%". trim($_GET['website']) ."%' and "; $condition->put('website',array('like','%'. trim($_GET['website']) .'%')); } if($_GET['project'] != '' && $_GET['project'] != 'Buyer') //$condition .=" project like '%". trim($_GET['project']) ."%' and "; $condition->put('project',array('like','%'. trim($_GET['project']) .'%')); if($_GET['mailfrom'] != '' && $_GET['mailfrom'] != 'mailfrom') //$condition .=" emailfrom like '%". trim($_GET['mailfrom']) ."%' and "; $condition->put('emailfrom',array('like','%'. trim($_GET['mailfrom']) .'%')); if(isset($_GET['dmoz']) && $_GET['dmoz'] != '') //$condition .=" dmoz = 'yes' and "; $condition->put('dmoz','yes'); if($_GET['homePR'] != '') $condition->put('homePR',array($_GET['homePR'],$_GET['homePR2'])); //if($_GET['homePR2'] != '') $condition .=" homePR <= {$_GET['homePR2']} and "; if($_GET['innerPR'] != '') //$condition .=" innerPR >= {$_GET['innerPR']} and "; $condition->put('innerPR',array($_GET['innerPR'],$_GET['innerPR'])); //if($_GET['innerPR2'] != '') $condition .=" innerPR <= {$_GET['innerPR']} and "; if(isset($_GET['response']) && $_GET['response'] == '1' && $_GET['stats'] == '请选择') $condition->put('stats',array('neq','无回应')); if($_GET['stats'] != '' && $_GET['stats'] != '请选择') //$condition .=" stats = \"". $_GET['stats'] ."\" and "; $condition->put('stats', $_GET['stats']); if(isset($_GET['active']) && $_GET['active'] == 1) $condition->put('active',array('in','0,1')); else $condition->put('active','1'); /**/ //echo $condition; $model = D("RelationView"); $count = $model->count($condition); if($count>0) { import("ORG.Util.Page"); //创建分页对象 if(!empty($_REQUEST['listRows'])) { $listRows = $_REQUEST['listRows']; }else { $listRows = ''; } $p = new Page($count,$listRows); //分页查询数据 $voList = $model->where($condition)->order($order.' id '.$sort)->limit($p->firstRow.','.$p->listRows)->findAll(); //分页跳转的时候保证查询条件 foreach($condition as $key=>$val) { if(is_array($val)) { foreach ($val as $t){ $p->parameter .= $key.'[]='.urlencode($t)."&"; } }else{ $p->parameter .= "$key=".urlencode($val)."&"; } } //分页显示 $page = $p->show(); //列表排序显示 $sortImg = $sort ; //排序图标 $sortAlt = $sort == 'desc'?'升序排列':'倒序排列'; //排序提示 $sort = $sort == 'desc'? 1:0; //排序方式 //模板赋值显示 $this->assign('list', $voList); $this->assign('sort', $sort); $this->assign('order', $order); $this->assign('sortImg', $sortImg); $this->assign('sortType', $sortAlt); $this->assign("page", $page); $status = array('请选择','待定','无回应','拒绝','成交','不成交','友情链接'); $this->assign('status', $status); } $this->display('index'); return; }
| anyShare分享到: | |
| |










There are no comments yet
Log in to post a comment.