原文:http://hi.baidu.com/jami918/item/a23b249fbe6a3dc9b6253144
1、
模型:Postsmodel
<?php
class Postsmodel extends CI_Model{ public function __construct() { parent::__construct(); } public function count_posts() { return $this->db->count_all('test'); } public function list_posts($limit,$offset) { $this->db->limit($limit,$offset); $query=$this->db->get('test'); return $query->result(); }}?>
控制器:Posts
<?php
class Posts extends CI_Controller{ function __construct() { parent::__construct(); $this->load->helper('url'); $this->load->library('pagination'); $this->load->model('Postsmodel','posts'); } function index($offset='') { $limit=2; $total = $this->posts->count_posts(); $data['posts']=$this->posts->list_posts($limit,$offset); $config['base_url']= base_url().'posts/index/'; $config['total_rows']=$total; $config['per_page']= $limit; $this->pagination->initialize($config); $data['pag_links'] = $this->pagination->create_links(); //echo $this->pagination->create_links(); $data['title']='Pagination'; $this->load->view('posts',$data); }}
视图:Posts
<h1>Pagination</h1><br><?php foreach($posts as $p) { ?> <div class="post"> <div class="title"><?php echo $p->id?></div> <div class="title"><?php echo $p->content;?></div> </div><?php echo $p->name; echo "<br>"; } echo $pag_links;?>
方式2:
控制器:Site
<?php
class Site extends Controller { function index() { $this->load->library('pagination'); $this->load->library('table'); //$this->table->set_heading('Id', 'The Title', 'The Content'); $config['base_url'] = 'http://localhost:8888/ci/index.php/site/index'; $config['total_rows'] = $this->db->get('data')->num_rows(); $config['per_page'] = 10; $config['num_links'] = 20; $config['full_tag_open'] = '<div id="pagination">'; $config['full_tag_close'] = '</div>'; $this->pagination->initialize($config); $data['records'] = $this->db->get('data', $config['per_page'], $this->uri->segment(3)); $this->load->view('site_view', $data); }}
模型:无
视图:site_view
<!DOCTYPE html>
<html lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>untitled</title> <style type="text/css" media="screen"> #container { width: 600px; margin: auto; font-family: helvetica, arial; } table { width: 600px; margin-bottom: 10px; } td { border-right: 1px solid #aaaaaa; padding: 1em; } td:last-child { border-right: none; } th { text-align: left; padding-left: 1em; background: #cac9c9; border-bottom: 1px solid white; border-right: 1px solid #aaaaaa; } #pagination a, #pagination strong { background: #e3e3e3; padding: 4px 7px; text-decoration: none; border: 1px solid #cac9c9; color: #292929; font-size: 13px; } #pagination strong, #pagination a:hover { font-weight: normal; background: #cac9c9; } </style></head><body> <div id="container"> <h1>Super Pagination with CodeIgniter</h1> <?php echo $this->table->generate($records); ?> <?php echo $this->pagination->create_links(); ?> </div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript" charset="utf-8"> $('tr:odd').css('background', '#e3e3e3');</script></body></html>
CI扩展分页类,使其适用于ajax分页
http://rabbit52.com/2012/devel/extending-pagination-library-for-ajax