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; }
}

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

原文地址:https://www.cnblogs.com/sunxun/p/4334064.html