不BB了,直接上代码
Model类
class Page_model extends CI_Model
{
public function get_page()
{
//这是一个sql语句,查询需要的结果,并将结果返回
$data = $this->db->select('id,name,age')->from('user')->get()->result_array();
return $data;
}
}
控制器类
class Pages extends CI_Controller
{
public function page(){
//载入分页类
$this->load->library('pagination');
$perPage=10;//每页10条
//配置项设置
$config['base_url']=site_url("pages/page");
$config['total_rows']=$this->db->count_all_results('user');
$config['per_page']=$perPage;
$config['uri_segment']=3;//偏移量,默认是3,如果在控制器有二级目录,根据偏移量层级而定
//自定义配置
$config['first_link']="首页";
$config['prev_link']="上一页";
$config['next_link']="下一页";
$config['last_link']="尾页";
//传入配置项,并生成链接
$this->pagination->initialize($config);
$data['links']=$this->pagination->create_links();
//设置偏移量
$offset=$this->uri->segment(3);
$this->db->limit($perPage,$offset);
//加载模型类和视图
$this->load->model("page_model","page");
$data['user']=$this->page->get_page();
$this->load->view("pages/page.html",$data);
}
}
View文件(page.html中的核心代码)
<table>
<tr>
<td>ID</td>
<td>名字</td>
<td>年龄</td>
</tr>
<?php foreach($user as $v):?>
<tr>
<td><?php echo $v['id'] ?></td>
<td><?php echo $v['name'] ?></td>
<td><?php echo $v['age'] ?></td>
</tr>
<?php endforeach?>
</table>
<?php echo $links ?>
效果图