CodeIgniter3.0的分页例程

首先创建具有以下内容的控制器文件controllers/Home.php  

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

class Home extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        // load Pagination library
        $this->load->library('pagination');

        // load URL helper
        $this->load->helper('url');
    }


    public function index()
	{
        // load db and model
        $this->load->database();
        $this->load->model('LogModel');

        // init params
        $params = array();
        $limit_per_page = 10;
        $start_index = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
        $total_records = $this->LogModel->get_total();

        if ($total_records > 0)
        {
            // get current page records
            $params["results"] = $this->LogModel->get_current_page_records($limit_per_page, $start_index);

            $config['base_url'] = base_url() . 'index.php/admin/home/index';
            $config['total_rows'] = $total_records;
            $config['per_page'] = $limit_per_page;
            $config["uri_segment"] = 4;

            $this->pagination->initialize($config);

            // build paging links
            $params["links"] = $this->pagination->create_links();
        }
        $this->load->view('admin/index',$params);
	}
}

接下来,我们需要一个模型文件models/LogModel.php ,该文件从log表中获取记录。

<?php

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

class LogModel extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    public function get_current_page_records($limit,$start)
    {
        $this->db->limit($limit, $start);
        $query = $this->db->get("log");

        if ($query->num_rows() > 0)
        {
            foreach ($query->result() as $row)
            {
                $data[] = $row;
            }

            return $data;
        }

        return false;

    }

    public function get_total()
    {
        return $this->db->count_all("log");
    }

}

最后,让我们在views/admin/index.php中创建一个显示用户列表的视图文件。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>m-Index</title>
</head>
<body>
<div class="layui-container">
<?php if (isset($results)) { ?>
<table class="layui-table">
    <colgroup>
        <col width="150">
        <col width="200">
        <col>
    </colgroup>

    <thead>
    <tr>
        <th>IP</th>
        <th>路径</th>
        <th>时间</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($results as $data) { ?>
        <tr>
            <td><?php echo $data->ip ?></td>
            <td><?php echo $data->url ?></td>
            <td><?php echo $data->time ?></td>
        </tr>
    <?php } ?>



    </tbody>
</table>
<?php } else { ?>
    <div>No log(s) found.</div>
<?php } ?>


<?php if (isset($links)) { ?>
    <?php echo $links ?>
<?php } ?>
</div>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

reg183

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

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

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

打赏作者

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

抵扣说明:

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

余额充值