thinkphp默认分页
html代码
<div class="f_r"> {$page} </div>
一:最简单的分页
$m = M('stock_log'); $page = I('get.page'); $order = 'id desc'; $where = ''; $count = $m->where($where)->count();// 查询满足要求的总记录数 $Page = new ThinkPage($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25) $show = $Page->show();// 分页显示输出 $list = $m->where($where)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select(); foreach($list as $k=>$v){ $list[$k]['local_add_time'] = date('Y-H-d H:i:s',$v['add_time']); } $this->assign('note',$list); $this->assign('page',$show);
二,分页样式
if(IS_POST){ $files['depot_id'] = I('post.depot_id') ; $files['keywords'] = I('post.keywords') ; } if($files['depot_id']){ $where['depot_id'] = $files['depot_id']; } if($files['keywords']){ $where['_string'] = ' (goods_name like "%'.$files['keywords'].'%") OR ( goods_barcode like "%'.$files['keywords'].'") '; } $this->assign('files',$files); $m = M('stock_log'); $page = I('get.page'); $order = 'id desc'; $pagesize = '15'; $count = $m->where($where)->count();// 查询满足要求的总记录数 $page = new ThinkPage($count,$pagesize);// 实例化分页类 传入总记录数和每页显示的记录数(25) $page->listRows = $pagesize; $page->rollPage = '10'; $page->lastSuffix=false; $page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录 每页<b>%LIST_ROW%</b>条 第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>'); $page->setConfig('prev','上一页'); $page->setConfig('next','下一页'); $page->setConfig('last','末页'); $page->setConfig('first','首页'); $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%'); $show = $page->show();// 分页显示输出 $list = $m->where($where)->order($order)->limit($page->firstRow.','.$page->listRows)->select(); foreach($list as $k=>$v){ $list[$k]['local_add_time'] = date('Y-H-d H:i:s',$v['add_time']); } $this->assign('note',$list); $this->assign('page',$show);
三,分页样式,与第二种有一点点差别,有文字输出
if(IS_POST){ $files['depot_id'] = I('post.depot_id') ; $files['keywords'] = I('post.keywords') ; } if($files['depot_id']){ $where['depot_id'] = $files['depot_id']; } if($files['keywords']){ $where['_string'] = ' (goods_name like "%'.$files['keywords'].'%") OR ( goods_barcode like "%'.$files['keywords'].'") '; } $this->assign('files',$files); $m = M('stock_log'); $page = I('get.page'); $order = 'id desc'; $pagesize = '15'; $count = $m->where($where)->count();// 查询满足要求的总记录数 $page = new ThinkPage($count,$pagesize);// 实例化分页类 传入总记录数和每页显示的记录数(25) $page->listRows = $pagesize; $page->rollPage = '10'; $show = $page->show();// 分页显示输出 $list = $m->where($where)->order($order)->limit($page->firstRow.','.$page->listRows)->select(); foreach($list as $k=>$v){ $list[$k]['local_add_time'] = date('Y-H-d H:i:s',$v['add_time']); } $this->assign('note',$list); $this->assign('page',$show);
自己写代码
public function purchase_list($page,$pagesize,$order,$files){ $m = M('purchase'); $link = ''; if($files['depot_id']){ $where['depot_id'] = $files['depot_id']; $link .= '&depot_id='.$files['depot_id']; } if($files['keywords']){ $where['purchase_sn'] = array('like','%'.$files['keywords'].'%'); $link .= '&keywords='.$files['keywords']; } $count = $m->where($where)->count(); $totalpage = ($count%$pagesize)>0?intval(($count/$pagesize))+1:$count/$pagesize; $page = empty($page)?1:$page; $firstpage = '1'; $prevpage = ($page-1)<1?1:($page-1); $nextpage = ($page+1)>$totalpage?$totalpage:($page+1); $lastpage = $totalpage; $url = U('pur/index'); $pages['count'] = $count; $pages['page'] = $page; $pages['totalpage'] = $totalpage; $pages['firstpage'] = $url."?page=".$firstpage.$link; $pages['prevpage'] = $url."?page=".$prevpage.$link; $pages['nextpage'] = $url."?page=".$nextpage.$link; $pages['lastpage'] = $url."?page=".$lastpage.$link; $list = $m->where($where)->page($page,$pagesize)->order('id desc')->select(); // 格式化字段 foreach($list as $k=>$v){ $list[$k]['local_time'] = date('Y-m-d',$v['time']); $list[$k]['local_total_price'] = local_price($v['total_price']); } $pur['list'] = $list; $pur['pages'] = $pages; $pur['files'] =$files; return $pur; }