服务器之家

服务器之家 > 正文

CI框架简单分页类用法示例

时间:2020-06-15 14:03     来源/作者:moqiang02

本文实例讲述了CI框架简单分页类用法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
 *
 * 关于 页码有效性的判断需要加在 控制器中判断,即当页码数<1或者>总页数
 *
 */
class Custom_pagination
{
  var $page_url = ''; //分页目标URL
  var $page_size = 10; //每一页行数
  var $page_num = 1;//页码
  var $rows_num= '';//数据总行数
  var $links_num= 3;//选中链接前后的链接数,必须大于等于1
 
  var $anchor_class= '';//链接样式类
  var $current_class= '';//当前页样式类
  var $full_tag_open= '';//分页开始标签
  var $full_tag_close= '';//分页结束标签
  var $info_tag_open= '';
  var $info_tag_close= ' ';
  var $first_tag_open= '';
  var $first_tag_close= ' ';
  var $last_tag_open= ' ';
  var $last_tag_close= '';
  var $cur_tag_open= ' <strong>';
  var $cur_tag_close= '</strong>';
  var $next_tag_open= ' ';
  var $next_tag_close= ' ';
  var $prev_tag_open= ' ';
  var $prev_tag_close= '';
  var $num_tag_open= ' ';
  var $num_tag_close= '';
 
  public function __construct($params = array())
  {
    if (count($params) > 0)
    {
      $this->init($params);
    }
  }
  
  function init($params = array()) //初始化数据
  {
    if (count($params) > 0)
    {
      foreach ($params as $key => $val)
      {
        if (isset($this->$key))
        {
          $this->$key = $val;
        }
      }
    }
  }
  
  function create_links()
  {
    ///////////////////////////////////////////////////////
    //准备数据
    ///////////////////////////////////////////////////////
    $page_url = $this->page_url;
    $rows_num = $this->rows_num;
    $page_size = $this->page_size;
    $links_num = $this->links_num;
 
    if ($rows_num == 0 OR $page_size == 0)
    {
      return '';
    }
 
    $pages = intval($rows_num/$page_size);
    if ($rows_num % $page_size)
    {
      //有余数pages+1
      $pages++;
    };
    $page_num = $this->page_num < 1 ? '1' : $this->page_num;
 
    $anchor_class = '';
    if($this->anchor_class !== '')
    {
      $anchor_class = 'class="'.$this->anchor_class.'" ';
    }
 
    $current_class = '';
    if($this->current_class !== '')
    {
      $current_class = 'class="'.$this->current_class.'" ';
    }
    if($pages == 1)
    {
      return '';
    }
    if($links_num < 0)
    {
      return '- -!links_num必须大于等于0';
    }
    ////////////////////////////////////////////////////////
    //创建链接开始
    ////////////////////////////////////////////////////////
    $output = $this->full_tag_open;
    $output .= $this->info_tag_open.'共'.$rows_num.'条数据 第 '.$page_num.'/'.$pages.' 页'.$this->info_tag_close;
    //首页
    if($page_num > 1)
    {
      $output .= $this->first_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url).'" rel="external nofollow" >首页</a>'.$this->first_tag_close;
    }
    //上一页
    if($page_num > 1)
    {
      $n = $page_num - 1;
      $output .= $this->prev_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$n).'" rel="external nofollow" rel="external nofollow" >上一页</a>'.$this->prev_tag_close;
    }
    //pages
    for($i=1;$i<=$pages;$i++)
    {
      $pl = $page_num - $links_num < 0 ? 0 : $page_num - $links_num;
      $pr = $page_num + $links_num > $pages ? $pages : $page_num + $links_num;
      //判断链接个数是否太少,举例,假设links_num = 2,则链接个数不可少于 5 个,主要是 当page_num 等于 1, 2 和 n,n-1的时候
      if($pr < 2 * $links_num + 1)
      {
        $pr = 2 * $links_num + 1;
      }
      if($pl > $pages-2 * $links_num)
      {
        $pl = $pages - 2 * $links_num;
      }
      if($i == $page_num)
      //current page
        $output .= $this->cur_tag_open.'<span '.$current_class.' >'.$i.'</span>'.$this->cur_tag_close;
      }else if($i >= $pl && $i <= $pr)
      {
        $output .= $this->num_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$i).'" rel="external nofollow" >'.$i.'</a>'.$this->num_tag_close;
      }
    }
    //下一页
    if($page_num < $pages)
    {
      $n = $page_num + 1;
      $output .= $this->next_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$n).'" rel="external nofollow" rel="external nofollow" >下一页</a>'.$this->next_tag_close;
    }
    //末页
    if($page_num < $pages)
    {
      $output .= $this->last_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$pages).'" rel="external nofollow" >末页</a>'.$this->last_tag_close;
    }
 
    $output.=$this->full_tag_close;
    return $output;
  }
}

控制器里调用

?
1
2
3
4
5
6
7
8
$config['page_url']
= 'about/science';
$config['page_size'] = $pagesize;
$config['rows_num'] = $num_rows;
$config['page_num'] = $page;
$this->load->library('Custom_pagination');
$this->custom_pagination->init($config);
echo $this->custom_pagination->create_links();
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
class page{
   
  public $page; //当前页
  public $pagenum; // 页数
  public $pagesize; // 每页显示条数
  public function __construct($count, $pagesize){
    $this->pagenum = ceil($count/$pagesize);
    $this->pagesize = $pagesize;
    $this->page =(isset($_GET['p'])&&$_GET['p']>0) ? intval($_GET['p']) : 1;
  }
  /**
   * 获得 url 后面GET传递的参数
   */
  public function getUrl(){  
    $url = 'index.php?'.http_build_query($_GET);
    $url = preg_replace('/[?,&]p=(\w)+/','',$url);
    $url .= (strpos($url,"?") === false) ? '?' : '&';
    return $url;
  }
  /**
   * 获得分页HTML
   */
  public function getPage(){
    $url = $this->getUrl();
    $start = $this->page-5;
    $start=$start>0 ? $start : 1; 
    $end  = $start+9;
    $end = $end<$this->pagenum ? $end : $this->pagenum;
    $pagestr = '';
    if($this->page>5){
      $pagestr = "<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=1".">首页</a> ";
    }
    if($this->page!=1){
      $pagestr.= "<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".($this->page-1).">上一页</a>";
    }
     
    for($i=$start;$i<=$end;$i++){
      $pagestr.= "<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".$i.">".$i."</a> ";           
    }
    if($this->page!=$this->pagenum){
      $pagestr.="<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".($this->page+1).">下一页</a>";
       
    }
    if($this->page+5<$this->pagenum){
      $pagestr.="<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".$this->pagenum.">尾页</a> ";
    }
    return $pagestr;  
  }
   
}
// 测试代码
$page = new page(100,10);
$str=$page->getPage();
echo $str;
?>

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

原文链接:https://blog.csdn.net/moqiang02/article/details/36006161

标签:

相关文章

热门资讯

歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
返回顶部