CI框架内置分页代码

Controller 控制器代码

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/*
		CI  框架内置分页
	*/
	public function __construct()
	{
		parent::__construct();
		$this->load->library('pagination'); //系统的library 
		$this->load->model('mpage_model','mpage');    //调数据库数据
		$this->load->helper('url');       //系统的帮助类
	}
	function index()
	{
		//总记录数
		$date=$this->mpage->gettotal();  
		$number=$date[0]->total;

		$config['base_url'] = site_url('Welcome/index/'); //路径
		$config['total_rows'] = $number;  //配置记录总条数        
		$config['per_page'] = 2; //配置每页显示的记录数

		//如果你希望在整个分页周围围绕一些标签,你可以通过下面的两种方法:
		//      $config['first_tag_open'] = '<div>';
		//        $config['first_tag_close'] = '</div>';
		$config['uri_segment'] = 3;		//指定第几参数为分页页数(默认是3 这个可不写)
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$config['last_link'] = '末页';
		$config['first_link'] = '首页';
		//配置分页导航当前页两边显示的条数
		$config['num_links'] = 3;
		//配置偏移量在url中的位置
		$config['cur_page'] = $this->uri->segment(3);
		//配置分页类

		$tab['table']=$this->mpage->get_books($config ['per_page'], $this->uri->segment(3));//当前页显示的数据

		$this->pagination->initialize($config);
		$this->load->view('index.html',$tab);		//调页面  传数据
	}



}

Model 模型层代码

<?php
class mpage_model extends CI_Model{
	function  __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	function  gettotal()
	{
		$query=$this->db->query("SELECT count(id) total FROM tickets");
		return $query->result();
	}
	function get_books($num,$offset)
	{
		$query=$this->db->get('tickets',$num,$offset);
		return $query->result();
	}
}

View 视图层代码

<table border=1>
  <tr>
    <th>火车车次</th>
    <th>余票</th>
  </tr>
<?php foreach ($table as $val) {?>
  <tr>
    <td><?php echo $val->train;?></td>
    <td><?php echo $val->num;?></td>
  </tr>
  <?php }	?>
</table>
<?php echo $this->pagination->create_links(); ?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汀风说后端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值