搜索
首页后端开发php教程thinkPHP分页功能图文详解
thinkPHP分页功能图文详解May 23, 2018 am 09:29 AM
phpthinkphp详解

这篇文章主要介绍了thinkPHP分页功能,结合完整实例形式分析了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 = &#39;service_@_&#39;.$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[&#39;status&#39;] = 0;
            $data[&#39;message&#39;] = &#39;收藏成功&#39;;
            break;
        case 1:
            $data[&#39;status&#39;] = 1;
            $data[&#39;message&#39;] = &#39;收藏失败&#39;;
            break;
        case 2:
            $data[&#39;status&#39;] = 2;
            $data[&#39;message&#39;] = &#39;已收藏&#39;;
            break;
        case 3:
            $data[&#39;status&#39;] = 3;
            $data[&#39;message&#39;] = &#39;未登陆&#39;;
            break;
        case 4:
            $data[&#39;status&#39;] = 4;
            $data[&#39;message&#39;] = &#39;缺少参数&#39;;
            break;
        default:
            $data[&#39;status&#39;] = 200;
            $data[&#39;message&#39;] = &#39;未知错误&#39;;
    }
    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 = &#39;&#39;) {
    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 = &#39;&#39;) {
    if( $this->_before_check($data) === false ){
      return $this->error[&#39;msg&#39;];
    }
    if(!empty($where) && is_array($where)){
      $condition[ $where[0] ] = array(&#39;eq&#39;, $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 = &#39;&#39;){
    //注册类型
    $info = CheckType($str);
    $condition[$info] = array(&#39;eq&#39;,$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[&#39;id&#39;] = array(&#39;eq&#39;,$this->userId);
    $condition[$info] = array(&#39;eq&#39;,$data);
    return $this->where($condition)->find();
  }
  /**
   * 修改用户密码
   * @param array $data[&#39;id&#39;]用户ID
   * @param $data[&#39;passWord&#39;]用户密码
   * return true or false
   */
  public function upUserPassById($data){
    $condition[&#39;id&#39;] = array(&#39;eq&#39;,$data[&#39;id&#39;]);
    $status = $this->where($condition)->save(array("password"=>md5($data[&#39;password&#39;])));
    if($status){
        return TRUE;
    }else {
        return FALSE;
    }
  }
  /**
   * 校验用户的账号或者密码是否正确
   * @param $data[&#39;username&#39;] 用户名
   * @param $data[&#39;password&#39;] 密码
   * return true or false
   */
  public function checkUserPasswd($data= array()){
      $type = CheckType($data[&#39;username&#39;]);
      $condition[$type] = array(&#39;eq&#39;,$data[&#39;username&#39;]);
      $condition[&#39;password&#39;] = array(&#39;eq&#39;,md5($data[&#39;password&#39;]));
       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[&#39;username&#39;]) && empty($data[&#39;username&#39;])){
      $this->error[&#39;msg&#39;] = &#39;请输入用户名&#39;;
      return false;
    }
    if(isset($data[&#39;nickname&#39;]) && empty($data[&#39;nickname&#39;])){
      $this->error[&#39;msg&#39;] = &#39;请输入昵称&#39;;
      return false;
    }
    if(isset($data[&#39;realname&#39;]) && empty($data[&#39;realname&#39;])){
      $this->error[&#39;msg&#39;] = &#39;请输入真名&#39;;
      return false;
    }
    if(isset($data[&#39;email&#39;]) && empty($data[&#39;email&#39;])){
      $this->error[&#39;msg&#39;] = &#39;请输入邮箱&#39;;
      return false;
    }
    if(isset($data[&#39;mobile&#39;]) && empty($data[&#39;mobile&#39;])){
      $this->error[&#39;msg&#39;] = &#39;请输入手机号码&#39;;
      return false;
    }
    if(isset($data[&#39;password&#39;]) && empty($data[&#39;password&#39;])){
      $this->error[&#39;msg&#39;] = &#39;请输入密码&#39;;
      return false;
    }
    if(isset($data[&#39;headimg&#39;]) && empty($data[&#39;headimg&#39;])){
      $this->error[&#39;msg&#39;] = &#39;请上传头像&#39;;
      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[&#39;onsale&#39;] = array(&#39;eq&#39;, $onsale); //是否上架
    $condition[&#39;status&#39;] = array(&#39;eq&#39;, $status); //状态
    $condition[&#39;id&#39;] = array(&#39;eq&#39;,$id);
    return $this->where($condition)->find();
  }
  /**
   * 商品列表
   * @param string $limit 查询条数
   * @param array $data 查询条件
   * @return array 二维数组
   */
  public function getProList($data = &#39;&#39;){
    $condition[&#39;onsale&#39;] = array(&#39;eq&#39;, $data[&#39;onsale&#39;]); //是否上架
    $condition[&#39;status&#39;] = array(&#39;eq&#39;, $data[&#39;status&#39;]); //状态
    $condition[&#39;type&#39;] = array(&#39;eq&#39;, $data[&#39;type&#39;]);  //分类
    if(isset($data[&#39;limit&#39;]) && isset($data[&#39;order&#39;]) ){
      $return =$this->where($condition)->limit($data[&#39;limit&#39;])->order($data[&#39;order&#39;])->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[&#39;id&#39;] = array(&#39;eq&#39;, $id);
    return $this->where($condition)->delete();
  }
  /**
   * 修改商品
   * @param string|int $id
   * @param array $data
   * @return
   */
  public function editProdcut($id, $data){
    $condition[&#39;id&#39;] = array(&#39;eq&#39;, $id);
    return $this->where($condition)->save($data);
  }
  public function getProductInfo($product){
    if(empty($product) || !isset($product[&#39;product_id&#39;])){
      return array();
    }
    $info = $this->getProOne($product[&#39;product_id&#39;]);
    $product[&#39;name&#39;] = $info[&#39;name&#39;];
    $product[&#39;store_id&#39;] = $info[&#39;store_id&#39;];
    $product[&#39;price&#39;] = $info[&#39;price&#39;];
    $product[&#39;m_price&#39;] = $info[&#39;m_price&#39;];
    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[&#39;total&#39;],$page,$limit);
    $this->assign(get_defined_vars());
    $this->display();
  }
  public function cancelCollect(){
    $ids = field(&#39;ids&#39;);
    $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 = &#39;&#39;, $maxpage = null) {
  $totalcount = $total;
  if (empty($url) || !is_string($url)) {
    $url = array();
    foreach ($_GET as $k => $v) {
      if ($k != &#39;page&#39;) {
        $url[] = urlencode($k) . &#39;=&#39; . urlencode($v);
      }
    }
    $url[] = &#39;page={page}&#39;;
    $url = &#39;?&#39; . implode(&#39;&&#39;, $url);
  }
  if ($total <= $perpage)
    return &#39;&#39;;
  $total = ceil($total / $perpage);
  $pagecount = $total;
  $total = ($maxpage && $total > $maxpage) ? $maxpage : $total;
  $page = intval($page);
  if ($page < 1 || $page > $total)
    $page = 1;
    $pages = &#39;<p class="pages"><a href="&#39; . str_replace(&#39;{page}&#39;, $page - 1 <= 0 ? 1 : $page - 1, $url) . &#39;" rel="external nofollow" title="上一页" class="page_start">上一页</a>&#39;;
  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 .= &#39;<a class="page-num" href="&#39; . str_replace(&#39;{page}&#39;, $i, $url) . &#39;" rel="external nofollow" >&#39; . $i . &#39;</a>&#39;;
    } else {
      $pages .= &#39;<span class="page_cur">&#39; . $i . &#39;</span>&#39;;
    }
    if ($maxi && $i >= $maxi) {
      $i = $total - 2;
      $maxi = 0;
    }
    if (($i == 2 or $total - 2 == $i) && $total > 10) {
      $pages .= &#39;&#39;;
    }
    if ($mini && $i >= 2) {
      $i = $mini;
      $mini = 0;
    }
  }
  $pages .= &#39;<a href="&#39; . str_replace(&#39;{page}&#39;, $page + 1 >= $total ? $total : $page + 1, $url) . &#39;" rel="external nofollow" title="下一页" class="page_next">下一页</a><span class="pageOp"><span class="sum">共&#39; . $totalcount .
      &#39;条 </span><input type="text" class="pages_inp" id="pageno" value="&#39; . $page . &#39;" onkeydown="if(event.keyCode==13 && this.value) {window.location.href=\&#39;&#39; . $url . &#39;\&#39;.replace(/\{page\}/, this.value);return false;}"><span class="page-sum">/ &#39; .
      $total . &#39;页 </span><input type="button" class="pages_btn" value="GO" onclick="if(document.getElementById(\&#39;pageno\&#39;).value>0)window.location.href=\&#39;&#39; . $url . &#39;\&#39;.replace(/\{page\}/, document.getElementById(\&#39;pageno\&#39;).value);"></span></p>&#39;;
  return $pages;
}

相关推荐:

php基于SQLite实现分页功能的方法

PHP+Ajax实现的无刷新分页功能的方法

thinkPHP实现多表查询及分页功能的方法

以上是thinkPHP分页功能图文详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
thinkphp是不是国产框架thinkphp是不是国产框架Sep 26, 2022 pm 05:11 PM

thinkphp是国产框架。ThinkPHP是一个快速、兼容而且简单的轻量级国产PHP开发框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。

一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列Apr 20, 2022 pm 01:07 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了关于使用think-queue来实现普通队列和延迟队列的相关内容,think-queue是thinkphp官方提供的一个消息队列服务,下面一起来看一下,希望对大家有帮助。

thinkphp的mvc分别指什么thinkphp的mvc分别指什么Jun 21, 2022 am 11:11 AM

thinkphp基于的mvc分别是指:1、m是model的缩写,表示模型,用于数据处理;2、v是view的缩写,表示视图,由View类和模板文件组成;3、c是controller的缩写,表示控制器,用于逻辑处理。mvc设计模式是一种编程思想,是一种将应用程序的逻辑层和表现层进行分离的方法。

实例详解thinkphp6使用jwt认证实例详解thinkphp6使用jwt认证Jun 24, 2022 pm 12:57 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了使用jwt认证的问题,下面一起来看一下,希望对大家有帮助。

thinkphp扩展插件有哪些thinkphp扩展插件有哪些Jun 13, 2022 pm 05:45 PM

thinkphp扩展有:1、think-migration,是一种数据库迁移工具;2、think-orm,是一种ORM类库扩展;3、think-oracle,是一种Oracle驱动扩展;4、think-mongo,一种MongoDb扩展;5、think-soar,一种SQL语句优化扩展;6、porter,一种数据库管理工具;7、tp-jwt-auth,一个jwt身份验证扩展包。

一文教你ThinkPHP使用think-queue实现redis消息队列一文教你ThinkPHP使用think-queue实现redis消息队列Jun 28, 2022 pm 03:33 PM

本篇文章给大家带来了关于ThinkPHP的相关知识,其中主要整理了使用think-queue实现redis消息队列的相关问题,下面一起来看一下,希望对大家有帮助。

thinkphp 怎么查询库是否存在thinkphp 怎么查询库是否存在Dec 05, 2022 am 09:40 AM

thinkphp查询库是否存在的方法:1、打开相应的tp文件;2、通过“ $isTable=db()->query('SHOW TABLES LIKE '."'".$data['table_name']."'");if($isTable){...}else{...}”方式验证表是否存在即可。

thinkphp怎么设置伪静态去除目录thinkphp怎么设置伪静态去除目录Dec 05, 2022 am 09:35 AM

thinkphp设置伪静态去除目录的方法:1、在httpd.conf配置文件中加载mod_rewrite.so模块;2、将httpd.conf中Allowoverride None的None改为All;3、修改对应的项目配置文件;4、在项目的根目录下面建立一个.htaccess文件即可。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)