某晚,自己写项目的时候去找资料,关于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. C# IOCP服务器

    C# IOCP服务器 @by  群63438968 这是我写的unity 网络斗地主的服务器源码,本来我想只公开部份服务端代码的,但是为了回报大家的热情,以及曾经和现在的好兄弟,我计划慢慢开源! 告诉 ...

  2. ios入门之c语言篇——基本函数——3——判断日期是一年的第几天

    3.判断日期是一年的第几天 参数返回值解析: 参数: y:int,年份: m:int,月份 d:int,日期 返回值: sum:传入日期是当年的第几天: 函数解析: leapyear(y);判断y是不 ...

  3. 硬盘IO,SAS,SATA,和HD TUNE

    SAS的接口技术可以向下兼容SATA.具体来说,二者的兼容性主要体现在物理层和协议层的兼容. SAS系统的背板(Backplane)既可以连接具有双端口.高性能的SAS驱动器,也可以连接高容量.低成本 ...

  4. dwr消息推送和tomcat集群

    网友的提问: 项目中用到了dwr消息推送.而服务端是通过一个http请求后 触发dwr中的推送方法.而单个tomcat中.服务器发送的http请求和用户都在一个tomcat服务器中.这样就能精准推送到 ...

  5. 【HDOJ】3329 The Flood

    超简单BFS. /* 3329 */ #include <iostream> #include <queue> #include <cstdio> #include ...

  6. AC+DP练习

    1.HDU 2222 Keywords Search 求目标串中出现了几个模式串. #include<iostream> #include<cstdio> #include&l ...

  7. -_-#setTimeout与setInterval

    你真的了解setTimeout和setInterval吗? 存在一个最小的时钟间隔 有关零秒延迟,此回调将会放到一个能立即执行的时段进行触发.JavaScript 代码大体上是自顶向下执行,但中间穿插 ...

  8. 【转】 Android的NDK开发(1)————Android JNI简介与调用流程

    原文网址:http://blog.csdn.net/conowen/article/details/7521340 ****************************************** ...

  9. 【模拟】Codeforces 691B s-palindrome

    题目链接: http://codeforces.com/problemset/problem/691/B 题目大意: 求一个字符串是不是镜像的(不是回文).是输出TAK否则RE. 题目思路: [模拟] ...

  10. 【模拟】Codeforces 704A & 705C Thor

    题目链接: http://codeforces.com/problemset/problem/704/A http://codeforces.com/problemset/problem/705/C ...