thinkPHP分页功能实例详解(2)

分页标题#e#

<?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><a href="' . str_replace('{page}', $page - 1 <= 0 ? 1 : $page - 1, $url) . '" title="上一页">上一页</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 href="' . str_replace('{page}', $i, $url) . '" >' . $i . '</a>'; } else { $pages .= '<span>' . $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) . '" title="下一页">下一页</a><span><span>共' . $totalcount . '条 </span><input type="text" value="' . $page . '" onkeydown="if(event.keyCode==13 &amp;&amp; this.value) {window.location.href=\'' . $url . '\'.replace(/\{page\}/, this.value);return false;}"><span>/ ' . $total . '页 </span><input type="button" value="GO"></span></div>'; return $pages; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/b8bcf0521ebf5580ce794a2fec6609d4.html