thinkPHP分页功能实例详解
本文实例讲述了thinkPHP分页功能。分享给大家供大家参考,具体如下:
interfaceServiceInterFace:
StaticService静态服务类:
abstractAbProduct 抽象商品管理类:
errorNum; switch($errorNum){ case0: $data['status']=0; $data['message']='收藏成功'; break; case1: $data['status']=1; $data['message']='收藏失败'; break; case2: $data['status']=2; $data['message']='已收藏'; break; case3: $data['status']=3; $data['message']='未登陆'; break; case4: $data['status']=4; $data['message']='缺少参数'; break; default: $data['status']=200; $data['message']='未知错误'; } return$data; }MemberModel会员模型:
userId=getUserInfo(0); } /** *实例化本类 *@returnMemberModel */ publicstaticfunctiongetInstance(){ returnStaticService::getInstance(__CLASS__); } /** *获取登录用户信息 *@paramstring$data查询条件 *@returnarray */ publicfunctiongetUser($data=''){ if(empty($data)){ return$this->where("id=".$this->userId)->find(); }else{ return$this->field($data)->where("id=".$this->userId)->find(); } } /** *修改用户信息 *@paramarray$data *@paramarray$where查询条件 */ publicfunctioneditUserInfo($data,$where=''){ if($this->_before_check($data)===false){ return$this->error['msg']; } if(!empty($where)&&is_array($where)){ $condition[$where[0]]=array('eq',$where[1]); return$this->where($condition)->save($data); } return$this->where("id=".$this->userId)->save($data); } /** *获取用户信息 *@paramstring$data用户名 *returnarray() */ publicfunctioncheckUserInfo($str,$field=''){ //注册类型 $info=CheckType($str); $condition[$info]=array('eq',$str); if(!empty($field)){ return$this->field($field)->where($condition)->find(); } return$this->where($condition)->find(); } /** *获取用户信息 *@paramarray$data用户名 *returnarray() */ publicfunctiongetAccount($data){ //注册类型 $info=CheckType($data); $condition['id']=array('eq',$this->userId); $condition[$info]=array('eq',$data); return$this->where($condition)->find(); } /** *修改用户密码 *@paramarray$data['id']用户ID *@param$data['passWord']用户密码 *returntrueorfalse */ publicfunctionupUserPassById($data){ $condition['id']=array('eq',$data['id']); $status=$this->where($condition)->save(array("password"=>md5($data['password']))); if($status){ returnTRUE; }else{ returnFALSE; } } /** *校验用户的账号或者密码是否正确 *@param$data['username']用户名 *@param$data['password']密码 *returntrueorfalse */ publicfunctioncheckUserPasswd($data=array()){ $type=CheckType($data['username']); $condition[$type]=array('eq',$data['username']); $condition['password']=array('eq',md5($data['password'])); return$this->where($condition)->find(); } /** *网页登录校验token *@paramtokenstring *returnbool */ publicfunctioncheckToken($token){ return$this->autoCheckToken($token); } /** *后台封号/解封 *paramint$user_id */ publicfunctionchangeStatus($data){ if($this->save($data)){ returntrue; }else{ returnfalse; } } protectedfunction_before_check(&$data){ if(isset($data['username'])&&empty($data['username'])){ $this->error['msg']='请输入用户名'; returnfalse; } if(isset($data['nickname'])&&empty($data['nickname'])){ $this->error['msg']='请输入昵称'; returnfalse; } if(isset($data['realname'])&&empty($data['realname'])){ $this->error['msg']='请输入真名'; returnfalse; } if(isset($data['email'])&&empty($data['email'])){ $this->error['msg']='请输入邮箱'; returnfalse; } if(isset($data['mobile'])&&empty($data['mobile'])){ $this->error['msg']='请输入手机号码'; returnfalse; } if(isset($data['password'])&&empty($data['password'])){ $this->error['msg']='请输入密码'; returnfalse; } if(isset($data['headimg'])&&empty($data['headimg'])){ $this->error['msg']='请上传头像'; returnfalse; } returntrue; } }ProductModel商品模型:
where($condition)->find(); } /** *商品列表 *@paramstring$limit查询条数 *@paramarray$data查询条件 *@returnarray二维数组 */ publicfunctiongetProList($data=''){ $condition['onsale']=array('eq',$data['onsale']);//是否上架 $condition['status']=array('eq',$data['status']);//状态 $condition['type']=array('eq',$data['type']);//分类 if(isset($data['limit'])&&isset($data['order'])){ $return=$this->where($condition)->limit($data['limit'])->order($data['order'])->select(); }else{ $return=$this->where($condition)->select(); } return$return; } /** *添加商品 *@paramarray$data *@returnint */ publicfunctionaddProduct($data){ return$this->add($data); } /** *删除商品 * */ publicfunctiondelProduct($id){ $condition['id']=array('eq',$id); return$this->where($condition)->delete(); } /** *修改商品 *@paramstring|int$id *@paramarray$data *@return */ publicfunctioneditProdcut($id,$data){ $condition['id']=array('eq',$id); return$this->where($condition)->save($data); } publicfunctiongetProductInfo($product){ if(empty($product)||!isset($product['product_id'])){ returnarray(); } $info=$this->getProOne($product['product_id']); $product['name']=$info['name']; $product['store_id']=$info['store_id']; $product['price']=$info['price']; $product['m_price']=$info['m_price']; return$product; } }ProductManage商品管理类:
getCollectList($page,$limit); $showpage=create_pager_html($list['total'],$page,$limit); $this->assign(get_defined_vars()); $this->display(); } publicfunctioncancelCollect(){ $ids=field('ids'); $return=ProductManage::getInstance()->cancelProductCollect($ids); exit(json_encode($return)); } }functions.php分页函数:
$v){ if($k!='page'){ $url[]=urlencode($k).'='.urlencode($v); } } $url[]='page={page}'; $url='?'.implode('&',$url); } if($total<=$perpage) return''; $total=ceil($total/$perpage); $pagecount=$total; $total=($maxpage&&$total>$maxpage)?$maxpage:$total; $page=intval($page); if($page<1||$page>$total) $page=1; $pages='上一页'; if($page>4&&$page<=$total-4){ $mini=$page-3; $maxi=$page+2; }elseif($page<=4){ $mini=2; $maxi=$total-2<7?$total-2:7; }elseif($page>$total-4){ $mini=$total-7<3?2:$total-7; $maxi=$total-2; } for($i=1;$i<=$total;$i++){ if($i!=$page){ $pages.=' '.$i.''; }else{ $pages.=' '.$i.''; } if($maxi&&$i>=$maxi){ $i=$total-2; $maxi=0; } if(($i==2or$total-2==$i)&&$total>10){ $pages.=''; } if($mini&&$i>=2){ $i=$mini; $mini=0; } } $pages.=' =$total?$total:$page+1,$url).'"rel="externalnofollow"title="下一页"class="page_next">下一页 共'.$totalcount. '条 /'. $total.'页 0)window.location.href=\''.$url.'\'.replace(/\{page\}/,document.getElementById(\'pageno\').value);">
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。