CodeIgniter3.0的分页例程

相关标签: # html# php
首先创建具有以下内容的控制器文件controllers/Home.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表中获取记录。
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
中创建一个显示用户列表的视图文件。
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<html lang="en">
<head>
<meta charset="utf-8">
<title>m-Indextitle>
head>
<body>
<div class="layui-container">
if (isset($results)) { ?>
<table class="layui-table">
<colgroup>
<col width="150">
<col width="200">
<col>
colgroup>
<thead>
<tr>
<th>IPth>
<th>路径th>
<th>时间th>
tr>
thead>
<tbody>
foreach ($results as $data) { ?>
<tr>
<td> echo $data->ip ?>td>
<td> echo $data->url ?>td>
<td> echo $data->time ?>td>
tr>
} ?>
tbody>
table>
} else { ?>
<div>No log(s) found.div>
} ?>
if (isset($links)) { ?>
echo $links ?>
} ?>
div>
body>
html>
文章来源: https://blog.51cto.com/u_13646572/5361511
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报