CodeIgniter 框架使用之分页使用

php代码部分

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Demo extends CI_Controller
{
    public function fenye()
    {
        //加载分页类
        $this->load->library('pagination');
        //完整的 URL 路径通向包含分页控制器类/方法
        $config['base_url'] = site_url('demo/fenye');
        //总记录数
        $count = $this->db->count_all_results('goods');
        //分页的数据总行数,查询数据库得到的数据总量。
        $config['total_rows'] = $count;
        //每页显示多条
        $config['per_page'] = '5';
        //放在你当前页码的前面和后面的“数字”链接的数量
        $config['num_links'] = 2;
        //整个分页周围围绕一些标签
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        //自定义起始链接
        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';

        //自定义结束链接
        $config['last_link'] = '尾页';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        //自定义“下一页”链接
        $config['next_link'] = '下一页;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        //自定义“上一页”链接
        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        //自定义“当前页”链接
        $config['cur_tag_open'] = '<li class="active"><a href="javascript:void(0)">';
        $config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
        //自定义“数字”链接
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        //初始化配置文件中设置参数来显示分页
        $this->pagination->initialize($config);
        //获取偏移量
        $offset = $this->uri->segment(3);

        $data = $this->db->limit($config['per_page'],$offset)->order_by('goods_id')->get('goods')->result_array();
        $res['data']=$data;
        $this->load->view('./home/index.html',$res);
    }
}

前端HTML部分

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="<?php echo base_url('');?>public/goods/bs/css/bootstrap.css">
	<style type="text/css">
		table tr th{
			text-align: center;
		}
	</style>
</head>
<body>
	<div class="container" style="margin-top: 60px;">
		<table class="table table-bordered text-center">
			<tr>
				<th>编号</th>
				<th>商品名称</th>
				<th>商品价格</th>
				<th>库存</th>
				<th>点击量</th>
				<th>操作</th>
			</tr>
			<?php foreach($data as $k=>$v):?>
			<tr>
				<td><?php echo $v['goods_id']?></td>
				<td><?php echo $v['goods_name']?></td>
				<td><?php echo $v['shop_price']?></td>
				<td><?php echo $v['goods_number']?></td>
				<td><?php echo $v['click_count']?></td>
				<td><a href="">编辑</a> | <a href="">删除</a></td>
			</tr>
			<?php endforeach;?>
		</table>
		<div class="page">
			<nav aria-label="Page navigation">
				<?php echo $this->pagination->create_links(); ?>
			</nav>
		</div>
	</div>
</body>
</html>

需要注意的是引入外部文件是ci框架的两处配置
autoload.php文件
$autoload[‘helper’] = array(‘url’);

config.php 文件
$config[‘base_url’] = ‘http://localhost/ci/’;

在这里插入图片描述在这里插入图片描述在前端代码中引入文件,具体代码可以参考前端代码部分

发布了40 篇原创文章 · 获赞 0 · 访问量 694

猜你喜欢

转载自blog.csdn.net/weixin_39218464/article/details/103769750