thinkPHP ページング関数の詳細な図による説明

墨辰丷
リリース: 2023-03-27 15:20:01
オリジナル
1156 人が閲覧しました

この記事では主に thinkPHP のページング機能を紹介し、製品モデルに基づいてページング機能を実装するための thinkPHP の関連操作テクニックを完全な例の形で分析します。この記事では、thinkPHP のページング機能について説明しています。参考として、次のように全員と共有します。

interface ServiceInterFace:

<?php
/**
 * InterFaceService
 * @author yhd
 */
namespace Red;
interface ServiceInterFace {
  /**
   * 实例化当前类
   */
  public static function getInstance();
}
ログイン後にコピー

StaticService Static Service Class:

<?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 Abstract Product Management Class:

<?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;
  }
ログイン後にコピー

メンバーモデルメンバーモデル:

<?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 ページネーション関数:

rrreええ

関連推奨事項: SQLite に基づいて

ページネーション関数

を実装する

phpメソッド

PHP+Ajaxメソッドでリフレッシュなしを実装する

ページネーション関数
thinkPHPメソッドでマルチテーブルクエリと

ページネーション関数を実装する


以上がthinkPHP ページング関数の詳細な図による説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!