thinkPHP分页功能实例详解

本文实例讲述了thinkPHP分页功能。分享给大家供大家参考,具体如下:

interface ServiceInterFace:

<?php /** * InterFaceService * @author yhd */ namespace Red; interface ServiceInterFace { /** * 实例化当前类 */ public static function getInstance(); }

StaticService 静态服务类:

<?php /** * 静态服务类 * StaticService * @author yhd */ namespace Red; class StaticService{ protected static $data; /** * 设置静态数据 * @param string $key key * @param mixed $data data * @return mixed */ public static function setData($key,$data){ self::$data[$key] = $data; return self::$data[$key]; } /** * 通过引用使用静态数据 * @param string $key key * @return mixed */ public static function & getData($key){ if(!isset(self::$data[$key])){ self::$data[$key] = null; } return self::$data[$key]; } /** * 缓存实例化过的对象 * @param string $name 类名 * @return 对象 */ public static function getInstance($name){ $key = 'service_@_'.$name; $model = &self::getData($key); if($model === null){ $model = new $name(); } return $model; } /** * html转义过滤 * @param mixed $input 输入 * @return mixed */ public static function htmlFilter($input){ if(is_array($input)) { foreach($input as & $row) { $row = self::htmlFilter($row); } } else { if(!get_magic_quotes_gpc()) { $input = addslashes($input); } $input = htmlspecialchars($input); } return $input; } }

abstract AbProduct  抽象商品管理类:

<?php /** * 抽象商品管理类 * AbProduct.class.php * @lastmodify 2015-8-17 * @author yhd */ namespace Red\Product; abstract class AbProduct{ public $errorNum; /* *返回错误信息 *@param $errorNum 错误代码 */ public function GetStatus(){ $errorNum = $this->errorNum; switch($errorNum){ case 0: $data['status'] = 0; $data['message'] = '收藏成功'; break; case 1: $data['status'] = 1; $data['message'] = '收藏失败'; break; case 2: $data['status'] = 2; $data['message'] = '已收藏'; break; case 3: $data['status'] = 3; $data['message'] = '未登陆'; break; case 4: $data['status'] = 4; $data['message'] = '缺少参数'; break; default: $data['status'] = 200; $data['message'] = '未知错误'; } return $data; }

MemberModel 会员模型:

<?php /** * 会员模型 * MemberModel.class.php * @copyright (C) 2014-2015 red * @license http://www.red.com/ * @lastmodify 2015-8-17 * @author yhd */ namespace Red\Passport\Models; use Think\Model; use Red\ServiceInterFace; use Red\StaticService; class MemberModel extends Model implements ServiceInterFace{ protected $userId; protected $error; protected function _initialize(){ $this->userId = getUserInfo(0); } /** * 实例化本类 * @return MemberModel */ public static function getInstance() { return StaticService::getInstance(__CLASS__); } /** * 获取登录用户信息 * @param string $data 查询条件 * @return array */ public function getUser($data = '') { if(empty($data)){ return $this->where("id=".$this->userId)->find(); }else{ return $this->field($data)->where("id=".$this->userId)->find(); } } /** * 修改用户信息 * @param array $data * @param array $where 查询条件 */ public function editUserInfo($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); } /** * 获取用户信息 * @param string $data 用户名 * return array() */ public function checkUserInfo($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(); } /** * 获取用户信息 * @param array $data 用户名 * return array() */ public function getAccount($data){ //注册类型 $info = CheckType($data); $condition['id'] = array('eq',$this->userId); $condition[$info] = array('eq',$data); return $this->where($condition)->find(); } /** * 修改用户密码 * @param array $data['id']用户ID * @param $data['passWord']用户密码 * return true or false */ public function upUserPassById($data){ $condition['id'] = array('eq',$data['id']); $status = $this->where($condition)->save(array("password"=>md5($data['password']))); if($status){ return TRUE; }else { return FALSE; } } /** * 校验用户的账号或者密码是否正确 * @param $data['username'] 用户名 * @param $data['password'] 密码 * return true or false */ public function checkUserPasswd($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 * @param token string * return bool */ public function checkToken($token){ return $this->autoCheckToken($token); } /** * 后台封号/解封 * param int $user_id */ public function changeStatus($data){ if($this->save($data)){ return true; }else{ return false; } } protected function _before_check(&$data){ if(isset($data['username']) && empty($data['username'])){ $this->error['msg'] = '请输入用户名'; return false; } if(isset($data['nickname']) && empty($data['nickname'])){ $this->error['msg'] = '请输入昵称'; return false; } if(isset($data['realname']) && empty($data['realname'])){ $this->error['msg'] = '请输入真名'; return false; } if(isset($data['email']) && empty($data['email'])){ $this->error['msg'] = '请输入邮箱'; return false; } if(isset($data['mobile']) && empty($data['mobile'])){ $this->error['msg'] = '请输入手机号码'; return false; } if(isset($data['password']) && empty($data['password'])){ $this->error['msg'] = '请输入密码'; return false; } if(isset($data['headimg']) && empty($data['headimg'])){ $this->error['msg'] = '请上传头像'; return false; } return true; } }

ProductModel 商品模型:

<?php /** * 商品模型 * ProductModel.class.php * @lastmodify 2015-8-17 * @author yhd */ namespace Red\Product\Models; use Think\Model; use Red\ServiceInterFace; use Red\StaticService; class ProductModel extends Model implements ServiceInterFace{ /** * 实例化本类 * @return ProductModel */ public static function getInstance() { return StaticService::getInstance(__CLASS__); } /** * 单个商品 * @param string $id * @param integer $status 状态 1:有效 2:无效 * @param integer $onsale 是否上架 1:是 2:否 * @return array 一维数组 */ public function getProOne($id, $status = 1 , $onsale = 1){ $condition['onsale'] = array('eq', $onsale); //是否上架 $condition['status'] = array('eq', $status); //状态 $condition['id'] = array('eq',$id); return $this->where($condition)->find(); } /** * 商品列表 * @param string $limit 查询条数 * @param array $data 查询条件 * @return array 二维数组 */ public function getProList($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; } /** * 添加商品 * @param array $data * @return int */ public function addProduct($data){ return $this->add($data); } /** * 删除商品 * */ public function delProduct($id){ $condition['id'] = array('eq', $id); return $this->where($condition)->delete(); } /** * 修改商品 * @param string|int $id * @param array $data * @return */ public function editProdcut($id, $data){ $condition['id'] = array('eq', $id); return $this->where($condition)->save($data); } public function getProductInfo($product){ if(empty($product) || !isset($product['product_id'])){ return array(); } $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 商品管理类:

<?php   namespace User\Controller;   use Red\Product\ProductManage;   class FavoriteController extends AuthController {   public function index($page=1){     $limit=1;     $list = ProductManage::getInstance()->getCollectList($page,$limit);     $showpage = create_pager_html($list['total'],$page,$limit);     $this->assign(get_defined_vars());     $this->display();   }   public function cancelCollect(){     $ids = field('ids');     $return = ProductManage::getInstance()->cancelProductCollect($ids);     exit(json_encode($return));   } }

functions.php 分页函数:

<?php /** * 分页 * @param $total 总条数 * @param $page 第几页 * @param $perpage 每页条数 * @param $url 链接地址 * @param $maxpage 最大页码 * @return string 最多页数 */ function create_pager_html($total, $page = 1, $perpage = 20, $url = '', $maxpage = null) { $totalcount = $total; if (empty($url) || !is_string($url)) { $url = array(); foreach ($_GET as $k => $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 = '<div class="pages"><a href="' . str_replace('{page}', $page - 1 <= 0 ? 1 : $page - 1, $url) . '" rel="external nofollow" title="上一页" class="page_start">上一页</a>'; 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 .= '<a class="page-num" href="' . str_replace('{page}', $i, $url) . '" rel="external nofollow" >' . $i . '</a>'; } else { $pages .= '<span class="page_cur">' . $i . '</span>'; } if ($maxi && $i >= $maxi) { $i = $total - 2; $maxi = 0; } if (($i == 2 or $total - 2 == $i) && $total > 10) { $pages .= ''; } if ($mini && $i >= 2) { $i = $mini; $mini = 0; } } $pages .= '<a href="' . str_replace('{page}', $page + 1 >= $total ? $total : $page + 1, $url) . '" rel="external nofollow" title="下一页" class="page_next">下一页</a><span class="pageOp"><span class="sum">共' . $totalcount . '条 </span><input type="text" class="pages_inp" id="pageno" value="' . $page . '" onkeydown="if(event.keyCode==13 && this.value) {window.location.href=\'' . $url . '\'.replace(/\{page\}/, this.value);return false;}"><span class="page-sum">/ ' . $total . '页 </span><input type="button" class="pages_btn" value="GO" onclick="if(document.getElementById(\'pageno\').value>0)window.location.href=\'' . $url . '\'.replace(/\{page\}/, document.getElementById(\'pageno\').value);"></span></div>'; return $pages; }

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

时间: 2024-08-27 05:48:48

thinkPHP分页功能实例详解的相关文章

Android开发中滑动分页功能实例详解

本文实例讲述了Android开发中滑动分页功能.分享给大家供大家参考,具体如下: android UI 往右滑动,滑动到最后一页就自动加载数据并显示 如图: Java代码: package cn.anycall.ju; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import andro

thinkphp命名空间用法实例详解_php实例

本文实例讲述了thinkphp命名空间用法.分享给大家供大家参考,具体如下: 新版本(3.2)中采用命名空间的方式定义和加载类库文件,解决多个模块之间的冲突问题,并实现了更加高效的自动加载机制. 需要给类库定义所在的命名空间,命名空间的路径和类库文件的目录一致,就可以实现类的自动加载,例如Org\Util\File类的定义为 namespace Org\Util; class File { } 其所在的路径是ThinkPHP/Library/Org/Util/File.class.php,我们实

TinyMCE汉化及本地上传图片功能实例详解_jquery

TinyMCE我就不多介绍了,这是下载地址:https://www.tinymce.com/download/ 下载下来是英文版,要汉化也很简单. 首先去网上随便下载个汉化包,然后把汉化包解压后的langs文件夹里的zh_CN.js拷到你下载的TinyMCE的langs文件夹中就行.最后在 tinymce.init中加上"language: "zh_CN","(后面会贴出代码) 本地图片上传我用到了Jquery中的uploadify和UI,所以需要引用jquery.

Android实现有道辞典查询功能实例详解_Android

本文实例讲述了Android实现有道辞典查询功能的方法.分享给大家供大家参考,具体如下: 这是我做的一个简单的有道Android的DEMO,只是简单的雏形.界面设计也有点丑陋呵呵~ 看看下面的效果图: 第一步:思路解析 从界面看一共用了三个控件EditText,Button,WebView.其实是四个,是当我们查询内容为空的时候用来提示的Toast控件. 我们在EditText输入查询内容,这里包括中文,英文.然后通过参数的形式,从http://dict.youdao.com/m取出数据把结果

JavaWeb实现文件上传下载功能实例详解_java

在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 文件上传概述 1.文件上传的作用 例如网络硬盘!就是用来上传下载文件的. 在智联招聘上填写一个完整的简历还需要上传照片呢. 2.文件上传对页面的要求 上传文件的要求比较多,需要记一下: 必须使用表单,而不能是超链接 表单的method必须是POST,而不能是GET 表单的enctype必须是multipart/form-data 在表单中添加file表单字段,即<input ty

JSP使用MVC模式完成删除和修改功能实例详解_JSP编程

本文实例讲述了JSP使用MVC模式完成删除和修改功能的方法.分享给大家供大家参考.具体如下: 目标: ① 进一步理解MVC模式: ② 掌握删除功能的基本实现过程: ③ 掌握修改功能的基本实现过程. 主要内容: ① 使用MVC完成删除功能: ② 使用MVC模式完成信息更新功能. 1.如何使用MVC模式完成删除功能 根据MVC模式的特点,分别考虑MVC的3个部分. ① 首先考虑V部分: 输入:通常删除功能是在查询的基础上完成的,所以在用户信息列表界面上可以添加删除的超链. 输出:提示用户删除是否成功

微信JS-SDK自定义分享功能实例详解【分享给朋友/分享到朋友圈】_javascript技巧

本文实例讲述了微信JS-SDK自定义分享功能.分享给大家供大家参考,具体如下: 分享出去的内容,可以通过jssdk进行修改. 1.配置jssdk Wx_config.html <?php import("@.ORG.jssdk"); $jssdk = new JSSDK(C('oauth_config.appid'), C('oauth_config.appsecret')); $signPackage = $jssdk->GetSignPackage(); ?> &

ThinkPHP分页类使用详解

 当网站的留言内容越来越多的时候,分页功能的应用就应运而生了,本文我们就来重点讲解下ThinkPHP框架自带的分页类的调用 一.首先需要在MsgManage控制器中加入分页方法   知识点: 1.count函数的试用 2.Page类实例化操作及相关参数了解 3.limit函数了用 4.show函数了解   编辑文件admin/Lib/Action/MsgManageAction.class.php   代码如下:   代码如下: class MsgManageAction extends Com

Illustrator实现分页功能教程详解

给各位Illustrator软件的使用者们来详细的解析分享一下实现分页功能的教程. 教程分享: 对矢量制图软件有所了解的朋友,一定知道Illustrator没有类似coredraw ,pagemaker等排版软件界面下方状态栏处的分页栏(如图一),但这不代表Illustrator无法制作分页哦! 相反illustrator制作印刷拼版非常舒服,还不知道此功能的朋友,不妨跟着Relen一起看看吧!   图一 Adobe Illustrator从早期版本就已经设置了 页面工具(page tool),