控制器
如想一直显示首页和尾页--》打开分页类318行改成if ($this->last_link !== FALSE)
首页229行改成 if ($this->first_link !== FALSE )
就是说去掉后边的and判断
public function index($p=1){ // $data['news'] = $this->news_model->get_news(); //echo $p; $data['title'] = '标题'; //分页使用 $res = $this->db->get('news');//进行一次查询 $config['base_url'] = '/ci/index.php/news/index';//设置分页的url路径 $config['uri_segment'] = 3; //设置url上第几段用于传递分页器的偏移量 $config['total_rows'] = $res->num_rows();//得到数据库中的记录的总条数 $config['per_page'] = '2';//每页记录数 $config['first_link'] = '首页'; $config['last_link'] = '尾页'; $config['full_tag_open'] = '<p>'; $config['full_tag_close'] = '</p>'; $config['next_link'] = '下一页'; $config['prev_link'] = '上一页'; $config['use_page_numbers'] = TRUE;//必须加上要不页码会乱... //$config['num_links'] = 4;//配置分页导航当前页两边显示的条数 $start=($p-1)*$config['per_page']; $this->pagination->initialize($config);//分页的初始化 //$data['news']= $this->news_model->get_news($config['per_page'],$this->uri->segment(3));//得到数据库记录 $data['news']= $this->news_model->get_news($config['per_page'],$start);//得到数据库记录 //分页结束 $this->load->view('templates/header', $data); $this->load->view('news/index', $data); $this->load->view('templates/footer'); } public function view($id){ $data['news_item'] = $this->news_model->view_news($id); if (empty($data['news_item'])) { show_404(); } $data['title'] = $data['news_item']['title']; $this->load->view('templates/header', $data); $this->load->view('news/view', $data); $this->load->view('templates/footer'); } public function create() { $this->load->helper('form'); $this->load->library('form_validation'); $data['title'] = 'Create a news item'; $this->form_validation->set_rules('title', 'Title', 'required'); $this->form_validation->set_rules('text', 'text', 'required'); if ($this->form_validation->run() === FALSE) { $this->load->view('templates/header', $data); $this->load->view('news/create'); $this->load->view('templates/footer'); } else { $this->news_model->set_news(); $this->load->view('news/success'); } }
模型
<?php class News_model extends CI_Model { public function __construct() { $this->load->database(); } public function get_news($num,$offset){ //从数据库中取数据,$num表示取得的数目,$offset表示从什么地方开始取 $query = $this->db->get('news',$num,$offset); return $query->result_array(); /* if ($slug === FALSE) { $query = $this->db->get('news'); return $query->result_array(); } $query = $this->db->get_where('news', array('slug' => $slug)); return $query->row_array(); */ } public function view_news($id) { $query = $this->db->get_where('news', array('id' => $id)); return $query->row_array(); } public function set_news() { $this->load->helper('url'); $slug = url_title($this->input->post('title'), 'dash', TRUE); $data = array( 'title' => $this->input->post('title'), 'slug' => $slug, 'text' => $this->input->post('text') ); return $this->db->insert('news', $data); } }
视图
<?php foreach ($news as $news_item): ?> <h2><?php echo $news_item['title'] ?></h2> <div id="main"> <?php echo $news_item['text'] ?> </div> <p><a href="/ci/index.php/news/view/<?php echo $news_item['id'] ?>"> View article 详细信息</a></p> <?php endforeach ?> <hr> <?php echo $this->pagination->create_links();?>