返回

CodeIgniter3.0的分页例程

发布时间:2022-09-29 01:41:20 280
# html# php

CodeIgniter3.0的分页例程_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>

 


特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线