某晚,自己写项目的时候去找资料,关于CI分页的,

发现百度出来的前几名的基本都是写的都是垃圾,

要么是实现不了,要么就是坑逼

所以我自己在这里写一个,不是很完美,只是说是简单的实现了原理

有了最基本的模型,相信大家都能在上面加上属于自己的东西,不多了

我用这个基本没用视图,视图基本就是CI最开始配置的一个基础的,所以就没有视图文件,都是在控制器中直接输出看值得

创建数据库 用的是mysql的

CREATE TABLE `type` (
`ty_id` int(11) NOT NULL auto_increment,
`ty_name` varchar(255) default NULL,
`ty_title` varchar(50) default NULL,
`ty_partent_id` varchar(50) NOT NULL default '0',
PRIMARY KEY (`ty_id`)
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

插入测试数据

INSERT INTO `type` VALUES ('2', '关于我', 'about', '0');
INSERT INTO `type` VALUES ('3', '个人日记', 'note', '0');
INSERT INTO `type` VALUES ('4', '心情语录', 'feeling', '0');
INSERT INTO `type` VALUES ('5', '个人下载', 'download', '0');
INSERT INTO `type` VALUES ('6', '优秀文章', 'novel', '0');
INSERT INTO `type` VALUES ('7', '留言墙', 'words', '0');
INSERT INTO `type` VALUES ('8', '人生感悟', null, '3');
INSERT INTO `type` VALUES ('9', '旅行见闻', null, '3');
INSERT INTO `type` VALUES ('10', '我的日记', null, '3');
INSERT INTO `type` VALUES ('11', '编程语录', null, '6');
INSERT INTO `type` VALUES ('12', 'Java真经', null, '6');
INSERT INTO `type` VALUES ('13', '修真电子书', null, '5');

控制器

welcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends MY_Controller {

	/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{ //其实这里可以不同加载,因为调用model的时候,只要让构造方法去调用就行了
$this->load->database(); $this->load->model('Type_model');
//装载分页类
$this->load->library('pagination');
//首页 //url路由类
$this->load->helper('url'); //进行配置
//每页显示的数据
$pagenum = 2;
$config['per_page'] = $pagenum;
//配置基础目录
$config['base_url']= site_url('welcome/index');
$config['base_url'] = "http://localhost/myblog/index.php/welcome/index/";
//一共多少条数据
$config['total_rows'] = $this->db->count_all('type'); //这里是自己需要分页的数据库名 $this->pagination->initialize($config);
//输出按钮
echo $this->pagination->create_links(); $offset = intval($this->uri->segment(3));
$id = $this->uri->segment(3); //获得url第三段字符
$id =$id ? $id:1;
$start = ($id - 1) * $pagenum; echo '$offset='.$offset.'<br>';
$data=$this->Type_model->get_books($start,$pagenum); foreach ($data as $value) {
echo '$value='.$value->ty_name;
} } /* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

模型中就一个方法 ,把这个方法添加到控制器对应的模型中就行了  type_model.php

class Type_model extends CI_Model {	
function get_books($start,$pagenum){
$sql = "select ty_id,ty_name from type limit $start,$pagenum"; $rs = $this->db->query($sql);
echo '最后一条sql语句是:'.$this->db->last_query();
$SonTypelist = $rs->result(); return $SonTypelist;
}
}

基本上有了这几段代码,实现的话基本是没有问题的了

Ci 简单分页,保证能实现的更多相关文章

  1. JavaScript简单分页,兼容IE6,~3KB

    简介 兼容IE6+及现代浏览器的简单分页,支持同一页面多个分页. 使用 Browser <link rel="stylesheet" href="css/GB-pa ...

  2. MVC简单分页

    对Car汽车表分页 实现简单分页,放在这里方便查看回顾,自定义每页几条有点问题,有待完善······ 1.新建mvc项目 2.添加linq to sql 数据库连接 3.添加CarBF类 using ...

  3. 使用Vs2005打造简单分页浏览器(1)原创

    原文:使用Vs2005打造简单分页浏览器(1)原创 使用Vs2005打造简单分页浏览器(1)原创1引言2功能3实现过程以及关键点4总结5不足之处6其他7 代码下载 1    引言很早就有搞一个浏览器的 ...

  4. MVC001之mvcpager简单分页

    描述:用mvcpager实现简单分页功能 参考网址: http://www.cnblogs.com/iamlilinfeng/archive/2013/03/11/2951460.html http: ...

  5. vue.js 2.0实现的简单分页

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  6. ASP.NET MVC 简单分页代码

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  7. Asp.net MVC 简单分页 自做简单分页

    Asp.net MVC 简单分页:   public static string Pager(int page,int pageSize,int total)         {           ...

  8. 通过 Django Pagination 实现简单分页

    作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 当博客上发布的文章越来越多时,通常需要进行分页显示,以免所有的文章都堆积在一个页面, ...

  9. DataGridView使用BindingNavigator实现简单分页功能

    接上一篇<DataGridView使用自定义控件实现简单分页功能>,本篇使用BindingNavigator来实现简单分页功能.其实也只是借用了一个BindingNavigator空壳, ...

随机推荐

  1. 取消开机window 设备选择

    问题:开机,出现window 设备选择,在win7 页面上会停留几秒.解决方案:win+R 输入 -msconfig 进入引导 把延迟改为3

  2. linux在shell中获取时间

    linux在shell中获取时间 获得当天的日期 date +%Y-%m-%d 输出: 2011-07-28 将当前日期赋值给DATE变量DATE=$(date +%Y%m%d) 有时候我们需要使用今 ...

  3. cf C. George and Number

    http://codeforces.com/problemset/problem/387/C 题意:给你一个大数,让你求个集合,可以通过操作得到这个数,求集合中个数最大值,操作 :从集合中任意取两个数 ...

  4. Spark应用程序的运行框架

    几个基本概念: (1)job:包含多个task组成的并行计算,往往由action催生. (2)stage:job的调度单位. (3)task:被送到某个executor上的工作单元. (4)taskS ...

  5. SVN:分支合并到主干

    合并日志: --- Merging r173674 through r175986 into '.': C src/test/java/com/test/rigel/sandbox/organizat ...

  6. Linux kernel ‘ip6_sk_dst_check’函数拒绝服务漏洞

    漏洞名称: Linux kernel ‘ip6_sk_dst_check’函数拒绝服务漏洞 CNNVD编号: CNNVD-201307-070 发布时间: 2013-07-05 更新时间: 2013- ...

  7. weblogic启动报错之Unrecognized option: -jrockit

    报错如下: $ ./startWebLogic.sh . . JAVA Memory arguments: -Xms512m -Xmx512m . WLS Start Mode=Production ...

  8. SharePoint Server 2007 Enterprise Key

    正式版 key SN: Tkjcb-3wkhk-2ty2t-qymk2-9xm2y 这个版本也是通过Key来区分是否是测试版还是正式版的 也就是说你输入正式版的Key他就是正式版,输入Enterpri ...

  9. Xshell中文编码的设置

    一直用的是SSH Secure Shell Client,关于中文乱码,一直找不到简便的解决方案,后来改用XShell,编码设置如下: 1.查看linux系统编码,linux命令: locale. 2 ...

  10. UVALive 5983 MAGRID

    题意:在一个n*m的网格上,从(0,0)走到(n-1,m-1),每次只能向右或者向下走一格.一个人最初有一个生命值x,走到每一个格生命值会变为x + s[i][j],(s[i][j]可为负,0,正), ...