Ci 简单分页,保证能实现
某晚,自己写项目的时候去找资料,关于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 简单分页,保证能实现的更多相关文章
- JavaScript简单分页,兼容IE6,~3KB
简介 兼容IE6+及现代浏览器的简单分页,支持同一页面多个分页. 使用 Browser <link rel="stylesheet" href="css/GB-pa ...
- MVC简单分页
对Car汽车表分页 实现简单分页,放在这里方便查看回顾,自定义每页几条有点问题,有待完善······ 1.新建mvc项目 2.添加linq to sql 数据库连接 3.添加CarBF类 using ...
- 使用Vs2005打造简单分页浏览器(1)原创
原文:使用Vs2005打造简单分页浏览器(1)原创 使用Vs2005打造简单分页浏览器(1)原创1引言2功能3实现过程以及关键点4总结5不足之处6其他7 代码下载 1 引言很早就有搞一个浏览器的 ...
- MVC001之mvcpager简单分页
描述:用mvcpager实现简单分页功能 参考网址: http://www.cnblogs.com/iamlilinfeng/archive/2013/03/11/2951460.html http: ...
- vue.js 2.0实现的简单分页
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- ASP.NET MVC 简单分页代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Asp.net MVC 简单分页 自做简单分页
Asp.net MVC 简单分页: public static string Pager(int page,int pageSize,int total) { ...
- 通过 Django Pagination 实现简单分页
作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 当博客上发布的文章越来越多时,通常需要进行分页显示,以免所有的文章都堆积在一个页面, ...
- DataGridView使用BindingNavigator实现简单分页功能
接上一篇<DataGridView使用自定义控件实现简单分页功能>,本篇使用BindingNavigator来实现简单分页功能.其实也只是借用了一个BindingNavigator空壳, ...
随机推荐
- laravel框架——线上环境错误总结
除了根目录,其他目录访问全是Not Found
- NSDate 总结日期操作
IOS Object-c NSDate总结日期操作 //NSDate //1, 创建NSDate对象 NSDate *nowDate = [NSDate date]; NSLog(@"%@& ...
- 原 IOS之NSValue整理
原 IOS之NSValue整理 发表于2年前(2013-02-28 23:02) 阅读(1974) | 评论(0) 5人收藏此文章, 我要收藏 赞3 IOS NSValue 值对象(value o ...
- 用硬件(Verilog)实现二进制码和格雷码的转换
格雷码(Gray code)是1880年由法国工程师Jean-Maurice-Emlle Baudot发明的一种编码,是一种绝对编码方式,典型格雷码是一种具有反射特性和循环特性的单步自补码,它的循环. ...
- Node.js RESTful API
什么是REST架构? REST表示代表性状态传输.REST是一种基于Web标准的架构,并使用HTTP协议. 它都是围绕着资源,其中每一个组件是资源和一个资源是由一个共同的接口使用HTTP的标准方法获得 ...
- CP30,DBCP数据源配置
Spring中 CP30数据源配置 <!-- 加载属性文件 01--> <bean id= "propertyConfigurer" class="or ...
- 获取Delphi所有类的类信息
Delphi遍历进程中所有Class的TypeInfo,即便是在implementation中的class或者其他 class的private的子class. 一般普通EXE中的TypeInfo存放在 ...
- Linux kernel ‘net/key/af_key.c’信息泄露漏洞
漏洞名称: Linux kernel ‘net/key/af_key.c’信息泄露漏洞 CNNVD编号: CNNVD-201307-071 发布时间: 2013-07-05 更新时间: 2013-07 ...
- [转] 关于C++中模板中的typename和class的区别比较
C++箴言:理解typename的两个含义 转自http://blog.csdn.net/dick_china/article/details/4522253 问题:在下面的 template dec ...
- 《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题
这个专栏开始介绍一些<ACM国际大学生程序设计竞赛题解>上的竞赛题目,读者可以配合zju/poj/uva的在线测评系统提交代码(今天zoj貌似崩了). 其实看书名也能看出来这本书的思路,就 ...