首先了解SQL语句中的limit用法
SELECT * FROM table …… limit 开始位置 , 操作条数 (其中开始位置是从0开始的)
例子:
取前20条记录:SELECT FROM table …… limit 0 , 20
从第11条开始取20条记录:SELECT FROM table …… limit 10 , 20
LIMIT n 等价于 LIMIT 0,n。
如select from table LIMIT 5; //返回前5行,和 select from table LIMIT 0,5一样
分页原理
所谓分页显示,也就是讲数据库中的结果集,一段一段显示出来
怎么分段,当前在第几段 (每页有几条,当前再第几页)
前10条记录:select from table limit 0,10
第11至20条记录:select from table limit 10,10
第21至30条记录:select * from table limit 20,10
分页公式:
(当前页数 - 1 )X 每页条数 , 每页条数
Select from table limit ($Page- 1) $PageSize, $PageSize
<?php namespace Page; /** * 自定义分页类 * 支持ajax */ class Mypage { private $cur_page;//当前页 private $total_rows;//总条数 private $per_page;//每页显示的条数 private $total_page;//总页数 private $base_url;//当前页面链接地址 private $first_page;//首页 private $prev_page;//上一页 private $next_page;//下一页 private $last_page;//尾页 private $num_links;//当前页前后显示几条页码 private $params;//分页所带参数 private $seg = 3; private $mode = '';//分页模式,为ajax时表示ajax分页 private $model_name = 'search_list';//为ajax分页时的js函数名称 /** * 构造函数初始化分页配置 * Mypage constructor. * @param array $params 分页配置数组 */ public function __construct(array $params = []) { if (count($params) > 0) { $this->initialize($params); log_message('info', 'Pagination Class Initialized'); } } /** * 初始化分页配置 * @param array $params 分页配置数组 */ public function initialize(array $params = []) { $this->cur_page = $params['cur_page'] ? intval($params['cur_page']) : 1; $this->total_rows = $params['total_rows'] ? intval($params['total_rows']) : 0; $this->per_page = $params['per_page'] ? intval($params['per_page']) : 12; $this->base_url = isset($params['base_url']) ? $params['base_url'] : ''; $this->first_page = isset($params['first_page']) ? $params['first_page'] : '首页'; $this->prev_page = isset($params['prev_page']) ? $params['prev_page'] : '上一页'; $this->next_page = isset($params['next_page']) ? $params['next_page'] : '下一页'; $this->last_page = isset($params['last_page']) ? $params['last_page'] : '尾页'; $this->num_links = isset($params['num_links']) ? $params['num_links'] : 2; $this->mode = isset($params['mode']) ? $params['mode'] : ''; $this->model_name = isset($params['model_name']) ? $params['model_name'] : 'search_list'; $this->params = isset($params['params']) ? $params['params'] : ''; $this->total_page = ceil($this->total_rows / $this->per_page); $this->_myset_url($this->base_url);//设置链接地址 } /** * 生成上一页页码地址 * [prev_page description] * @return [type] [description] */ private function prev_page() { if ($this->cur_page > 1 && $this->total_page != 1) { return $this->_get_link($this->_get_url($this->cur_page - 1), $this->prev_page, 'prev'); } return '<span class="prev">' . $this->prev_page . '</span>'; } /** * 生成下一页页码地址 * [next_page description] * @return [type] [description] */ private function next_page() { if ($this->cur_page < $this->total_page) { return $this->_get_link($this->_get_url($this->cur_page + 1), $this->next_page, 'next'); } return '<span class="next">' . $this->next_page . '</span>'; } /** * 生成第一页页码地址 * [get_first_page description] * @return [type] [description] */ private function get_first_page() { if ($this->cur_page > 1 && $this->total_page != 1) { return $this->_get_link($this->_get_url(1), $this->first_page, 'first'); } return '<span class="first">' . $this->first_page . '</span>'; } /** * 生成最后一页页码地址 * @return string */ private function get_last_page() { if ($this->cur_page < $this->total_page) { return $this->_get_link($this->_get_url($this->total_page), $this->last_page, 'end'); } return '<span class="end">' . $this->last_page . '</span>'; } /** * 生成中间数字页码 * [now_bar description] * @return [type] [description] */ private function now_bar($nowindex_style = 'pg_curr current') { $plus = $this->num_links; if ($this->cur_page > $plus) { $ben = $this->cur_page - $plus; $end = $this->cur_page + $plus; if ($end > $this->total_page) { $ben = ($this->total_page - 2 * $plus) > 0 ? ($this->total_page - 2 * $plus) : 1; $end = $this->total_page; } } else { $ben = 1; $end = $ben + 2 * $plus; $end = ($end > $this->total_page) ? $this->total_page : $end; } $out = ''; for ($i = $ben; $i <= $end; $i++) { if ($i == $this->cur_page) { $out .= '<span class="' . $nowindex_style . '">' . $i . '</span>'; } else { $out .= $this->_get_link($this->_get_url($i), $i, 'num'); } } return $out; } /** * 生成下拉框页码选择 * [select_page description] * @return [type] [description] */ private function select_page() { $url = $this->base_url . '/'; $out = '<select name="pagelect" class="pgselect" data-uri="' . $this->base_url . '" data-param="' . $this->params . '">'; if ($this->mode == 'ajax') { $out = '<select onchange="' . $this->model_name . '(this.value);return false;">'; } for ($i = 1; $i <= $this->total_page; $i++) { if ($i == $this->cur_page) { $out .= '<option selected value="' . $i . '">' . $i . '</option>'; } else { $out .= '<option value="' . $i . '">' . $i . '</option>'; } } $out .= '</select>'; return $out; } /** * 输出分页 * [show description] * @return [type] [description] */ public function show($type = 1) { $page = ''; $common = $this->get_first_page() . $this->prev_page() . $this->now_bar() . $this->next_page() . $this->get_last_page(); $des = '<span class="rows hidden-xs">' . $this->show_total() . $this->show_total_page() . '</span>'; $select = ' 第' . $this->select_page() . '页'; switch ($type) { case 1
文章转载:https://www.cnblogs.com/phproom/p/9588656.html
发表评论 取消回复