首先创建具有以下内容的控制器文件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>