情景:
全网搜索,结果是不同的栏目,不同的数据表联合筛选的结果,此时tp6自带的分页不太好使。
解决:
我们使用数组分页,参考手册中page的做法。
public function search(){ $data=input('get.'); if(empty($data['search_key'])){ return alert('请填写搜索关键字','/',3); } if($data['search_type']!=1){ return redirect((string) url('search_number',$data)); } $page=$data['page'];//第几页 $pagenumber=10;//每页显示数量 //所有筛选结果,用来统计总计数量 $searchDataT=Db::field('id,title,dbname,time') ->table('qing_draw') ->where('title','like','%'.$data['search_key'].'%') ->union([ 'SELECT id,title,dbname,time FROM qing_soft WHERE title LIKE "%'.$data['search_key'].'%"', 'SELECT id,title,dbname,time FROM qing_standard WHERE title LIKE "%'.$data['search_key'].'%"', ])->select()->toArray(); //当前页的筛选结果 $searchData=Db::field('id,title,dbname,time') ->table('qing_draw') ->where('title','like','%'.$data['search_key'].'%') ->union([ 'SELECT id,title,dbname,time FROM qing_soft WHERE title LIKE "%'.$data['search_key'].'%"', 'SELECT id,title,dbname,time FROM qing_standard WHERE title LIKE "%'.$data['search_key'].'%"', ])->page($page,$pagenumber)->select()->toArray(); $pageTotal=count($searchDataT);//总条数 $pageCount=ceil($pageTotal/$pagenumber);//一共多少页 return view('',[ 'searchData'=>$searchData, 'search_key'=>$data['search_key'], 'searchType'=>$data['search_type'], 'pageCount'=>$pageCount, 'page'=>$page ]); }
html:
{php} for($i=1;$i<=$pageCount;$i++){ if($page==$i){ $pageitem_current='pageitem_current'; }else{ $pageitem_current=''; } echo "<a class='pageitem $pageitem_current' href='/index/index/search?search_key=$search_key&search_type=$searchType&page=$i'>$i</a>"; } {/php}