修改autoload.php加载需要用的到组件:
$autoload['helper'] = array('url');
$autoload['libraries'] = array('layout', 'database', 'pagination'
controllers下新建posts.php:
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Posts extends CI_Controller {
- // function __construct() {
- // parent::__construct();
- // $config = array('layout' => 'layout');
- // $this->load->library('Layout', $config, 'layout');
- // }
- function index() {
- // $this->output->enable_profiler(TRUE);
- // $this->load->helper('url');
- $this->load->model('posts_model');
- $per_page = 3;
- $total = $this->posts_model->count_posts();
- $data['posts'] = $this->posts_model->get_posts($per_page, $this->uri->segment(3));
- $base_url = site_url('posts/index');
- $config['base_url'] = $base_url;
- $config['total_rows'] = $total;
- $config['per_page'] = $per_page;
- $config['uri_segment'] = '3';
- // $config['first_link'] = 'First';
- // $config['last_link'] = 'Last';
- // $config['next_link'] = '>';
- // $config['prev_link'] = '<';
- // $config['display_pages'] = FALSE;
- $this->pagination->initialize($config);
- $data['title_for_layout'] = 'My Posts';
- $this->layout->view('posts_view', $data);
- }
- }
- <?php
- class Posts_model extends CI_Model {
- function get_posts($limit = NULL, $offset = NULL) {
- $this->db->limit ($limit, $offset);
- return $this->db->get('posts');
- }
- function count_posts() {
- return $this->db->count_all_results('posts');
- }
- }
- <br>
- <table width="600px" class="my_table">
- <tr>
- <td width="100px"><b>Title</b></td>
- <td><b>Content</b></td>
- </tr>
- <?php
- foreach ($posts->result() as $post):
- echo '<tr>';
- echo '<td>' . $post->title . '</td>';
- echo '<td>' . $post->content . '</td>';
- echo '</tr>';
- endforeach;
- ?>
- </table>
- <?php echo $this->pagination->create_links(); ?>
- <br>
- <br>
