CodeIgniter分页教程
分页的简单教程
要想分页先必须获取所有数据。
对于某个表的不带条件的count,可以简单的用
$total = $this->db->count_all($table_name)
来获取,但是如果有条件,则要用count_all_results
$this->db->where(‘sex’, ‘M’);
$this->db->from(‘user’);
$total = $this->db->count_all_results();
1 2 3 4 5 6 7 8 9 10 11 12 |
// 加载分页类 $this->load->library('pagination'); $config['base_url'] = base_url().'index.php/books/index/'; $config['total_rows'] = $this->db->count_all(<span style="font-family: mceinline;">$table_name</span>); //每页数据条数 $config['per_page'] = 5; $config['uri_segment'] = 3; // 表示第 3 段 URL 为当前页数,如 index.php/控制器/方法/页数,如果表示当前页的 URL 段不是第 3 段,请修改成需要的数值。 $config['full_tag_open'] = '<p>'; $config['full_tag_close'] = '</p>'; $this->pagination->initialize($config); |
下面是完整案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$news_result=$this->db->order_by('id desc')->get('news')->result_array(); // 加载分页类 $count = $this->db->count_all('news'); $this->load->library('pagination'); $config['base_url'] = '/article/news'; $config['total_rows'] = $count; //每页数据条数 $config['per_page'] = 16; $config['uri_segment'] = 3; // 表示第 3 段 URL 为当前页数,如 index.php/控制器/方法/页数,如果表示当前页的 URL 段不是第 3 段,请修改成需要的数值。 $config['first_link'] = '首页'; $config['last_link'] = '尾页'; $config['prev_link'] = '上一页'; $config['next_link'] = '下一页'; $config['full_tag_open'] = '<p>'; $config['full_tag_close'] = '</p>'; $this->pagination->initialize($config); $pages = $this->pagination->create_links(); $this->load->view('article/news',array("pages"=>$pages,'news_list'=>$news_result)); |
- Apache安装配置PHP环境(安装版)
- codeigniter(ci)数据库操作教程简单增删改查