某晚,自己写项目的时候去找资料,关于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. iOS证书详解--再转

    一.成员介绍1.    Certification(证书)证书是对电脑开发资格的认证,每个开发者帐号有一套,分为两种:1)    Developer Certification(开发证书)安装在电脑上 ...

  2. coroutine协程

    如果你接触过lua这种小巧的脚本语言,你就会经常接触到一个叫做协程的神奇概念.大多数脚本语言都有对协程不同程度的支持.但是大多编译语言,如C/C++,根本就不知道这样的东西存在.当然也很多人研究如何在 ...

  3. cut 命令

    今天看到cut拿来取参数也是很方便的. cut -d = -f 2 -d表示分隔符 -f参数是分隔符算第几个参数

  4. BZOJ 3196 二逼平衡树

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:1.查询k在区间内的排名2.查询区间内排名为k的值3.修改某一位值上的数值4.查询k在区间内的 ...

  5. The Wedding Juicer

    poj2227:http://poj.org/problem?id=2227 题意:给你一块矩形区域,这个矩形区域是由一个个方格拼起来的,并且每个方格有一个高度.现在给这个方格灌水,问最多能装多少水. ...

  6. 获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName

    http://blog.csdn.net/bichenggui/article/details/4774457 -------------------------------------------- ...

  7. mysql存储过程写法—动态参数运用

    --删除 双击代码全选 1 drop procedure if exists up_common_select --创建 双击代码全选 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  8. Yii widget使用

    关于widgets,他们在yii中的关系如下 system.web.widgets 系统自带最基本的widget zii.widgets 是基本扩展 zii.widgets.grid 是基本扩展的重要 ...

  9. C#中实现邮件发送功能

    public static int sendmail(string to, string body,string subject) { try { int nContain = 0; ///添加发件人 ...

  10. hdu-4612-Warm up(边双连通分量--有重边)

    题意:有N 个点,M条边,加一条边,求割边最少.(有重边) 分析:先求双连通分量,缩点形成一个生成树,然后求这个的直径,割边-直径即是答案 因为有的图上可能有重边,这样不好处理.我们记录每条边的标号( ...