最近在学习B/S,选择了PHP CI框架作为切入点。

在尝试制作个人CMS的时候遇到了需要分页的情况,网上好像搜不到3.0版本以上的例子,下面附上本地实验的代码,供参考。

数据库情况如下:

首先看Controller

<?php
/**
* Created by PhpStorm.
* User: erdao
* Date: 16-1-11
* Time: 下午10:25
*/ class P extends CI_Controller
{ /**
* P constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('article_model','article');
$this->load->library('pagination');
} /**
* @param int $page 可看做offset
*/
public function index($page=0)
{
//每页显示三条数据
$limit['num']=3;
$limit['offset']=$page; $config['base_url']=site_url('p/index');
$config['total_rows']=$this->article->get_articles_num();//数据总条数
$config['per_page']=$limit['num'];//每页显示条数 $this->pagination->initialize($config); $data=array(
'articles'=>$this->article->get_limit_articles($limit)
); $this->load->view('page_ex',$data);
}
}

再来Model

<?php
/**
* Created by PhpStorm.
* User: erdao
* Date: 16-1-12
* Time: 下午9:48
*/ class Article_model extends CI_Model
{ /**
* Article_model constructor.
*/
public function __construct()
{
parent::__construct();
} /**
* 获取全部数据
* @return mixed
*/
public function get_all_articles()
{
$this->db->from('my_article');
$this->db->order_by('posttime', 'DESC');
$query=$this->db->get();
return $query->result_array();
} /**
* 获取表内数据数量
* @return mixed
*/
public function get_articles_num()
{
return $this->db->count_all('my_article');
} /**
* 获取有限个数的数据
* @param array $arr
* @return mixed
*/
public function get_limit_articles($arr=array('num'=>FALSE,'offset'=>FALSE))
{
if(isset($arr['num']) and isset($arr['offset']) and ($arr['num']!==FALSE) and ($arr['offset']!==FALSE))
{
$query=$this->db->get('my_article',$arr['num'],$arr['offset']);
return $query->result_array();
}
else
{
return $this->get_all_articles();
}
}
}

最后是view

<?php
foreach($articles as $item)
{
echo $item['title'];
} echo $this->pagination->create_links();

附上运行效果截图

需要注意的是,index/9  这里面的9可以看做是数据库中的索引(index),而不是页数

个人博客:http://www.dingshuo89.top

CodeIgniter(CI 3.0)分页类实践记录的更多相关文章

  1. Ci 自己的分页类【原创】

    这里是自己手写的一个CI分页类的实现 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** ...

  2. YII 1.0 分页类

    在控制器中 方法1 $criteria = new CDbCriteria();//AR的另一种写法 $model = Article::model(); $total = $model->co ...

  3. CI 分页类的使用

    分页本身很简单,无非就是一个 [limit $offset, $length] 的过程. $length 是每页显示的数据量,这个是固定的.要确定的就只有 $offset了. 在CI中的分页类同样要依 ...

  4. CI中的分页

    根据MVC的思想,分页是需要传数据到模型中,把页码传过去,在模型中根据页码分配: 更多分页类函数可以通过CI手册的分页类查看: $this -> load ->library('pagin ...

  5. php-数据库-分页类-上传类

    config.ini.php <?php header("content-type:text/html;charset=utf-8"); //项目的根目录 define(&q ...

  6. ***CI分页:为CodeIgniter写的分页类

    ? 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 ...

  7. codeigniter分页类传多个参数(转)

    http://example.com/index.php/控制器/方法名/页面的偏移值 页面的偏移值必须是方法名后第一个参数,否者分页类不能判断当前是哪一页,而用ci的分页类进行页面跳转时他是把偏移值 ...

  8. Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性

    在 Spring Boot 2 实践记录之 条件装配 一文中,曾经使用 Condition 类的 ConditionContext 参数获取了配置文件中的配置属性.但那是因为 Spring 提供了将上 ...

  9. CI框架分页类

    分页类1.分页类参数说明 'base_url' => 指向你的分页所在的控制器类/方法的完整的 URL, 'total_rows' => 数据的总行数, 'per_page' => ...

随机推荐

  1. LINUX ping 指定网卡

    计算机上有多块网卡(例如笔记本电脑的无线网卡和以太网卡)连接至网络,使用ping命令想指定使用哪块网卡怎么办? ping -I eth0 10.10.10.1ping -I eth1 10.10.10 ...

  2. 学java入门到精通,不得不看的15本书

    学java入门到精通,不得不看的15本书 一.Java编程入门类1.<Java编程思想>2.<Agile Java>中文版 二.Java编程进阶类1.<重构 改善既有代码 ...

  3. Win7下安装Apache+PHP+phpMyAdmin+wordpress+drupal+discuz

    [Apache] Listen 90 DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs&q ...

  4. WinServer 之 访问同网段服务器 或 同一服务器多虚拟机间的访问

    今天在发布网站时遇到不能访问同网段数据库服务器.具体情况为:web服务端部署在192.168.10.1下,而数据库服务端部署在192.168.10.2下,数据库连接直接配置为192.168.10.2会 ...

  5. Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include

    来自博客园开发团队开发前线最新消息: 在Entity Framework中,如果实体A关联了实体B,你想在加载实体A的同时加载实体B.通常做法是在LINQ查询中使用Include().但是,如果你在查 ...

  6. JSON3-翻译(不当之处,请指正)

    http://bestiejs.github.io/json3/JSON 3 显示了两个功能:stringify():序列化一个javaScript值为一个JSON,和parse():将一个json字 ...

  7. Python一些难以察觉的错误

    Python一些难以察觉的错误 今天把微博的收藏夹打开,发现以前很多收藏的好文章还没有细细研究,今天开始要慢慢研究总结总结.今天看的这篇文章地址: http://blog.amir.rachum.co ...

  8. 关于bochs用X11启动的说明

    关于网络上有很多关于 bochs启动时 连接到X Windows 报错: cannot connect to X display . 昨天搞了半天+晚上 + 今天早上 + 直到现在 才搞懂. 主要的原 ...

  9. 51nod 平均数(二分+树状数组)

    题目链接: 平均数 基准时间限制:4 秒 空间限制:131072 KB 分值: 80 LYK有一个长度为n的序列a. 他最近在研究平均数. 他甚至想知道所有区间的平均数,但是区间数目实在太多了. 为了 ...

  10. CF 118E Bertown roads 桥

    118E Bertown roads 题目:把无向图指定边的方向,使得原图变成有向图,问能否任意两点之间互达 分析:显然如果没有桥的话,存在满足题意的方案.输出答案时任意从一个点出发遍历一遍即可. 求 ...