Codeigniter 3 blog: how can I use the pagination from the index() method inside the search() method?(Codeigniter 3 博客:如何在 search() 方法中使用 index() 方法的分页?)
问题描述
我正在 Codeigniter 3.1.8 中开发一个基本的博客应用程序.帖子分页.该应用程序使用 pagination.php
配置文件.
I am working on a basic blog application in Codeigniter 3.1.8. The posts are paginated. The application uses a pagination.php
configuration file.
有一个用于搜索帖子的搜索框.我希望对搜索结果进行分页,并且由于方法 index()
和 search()
在同一个 Posts 控制器,我正在寻找一种方法,通过在搜索结果中使用 index()
内的分页来避免代码冗余.
There is a search box for searching posts. I wish to paginate the search results and, since the methods index()
and search()
are in the same Posts controller, I am looking for a way avoid code redundancy by using the pagination inside index()
for the search results too.
在我的帖子控制器中:
class Posts extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Static_model');
$this->load->model('Posts_model');
$this->load->model('Categories_model');
$this->load->model('Comments_model');
}
public function index() {
//load and configure pagination
$this->load->library('pagination');
$config['base_url'] = base_url("/posts");
$config['query_string_segment'] = 'page';
$config['total_rows'] = $this->Posts_model->get_num_rows();
$config['per_page'] = 12;
if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
$_GET[$config['query_string_segment']] = 1;
}
$limit = $config['per_page'];
$offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
$this->pagination->initialize($config);
$data = $this->Static_model->get_static_data();
$data['categories'] = $this->Categories_model->get_categories();
$data['posts'] = $this->Posts_model->get_posts($limit, $offset);
$this->load->view('partials/header', $data);
$this->load->view('posts');
$this->load->view('partials/footer');
}
public function search() {
$this->form_validation->set_rules('search', 'Search term', 'required|trim|min_length[3]');
$this->form_validation->set_error_delimiters('<p class = "error search-error"> ', ' </p>
');
// If search fails
if ($this->form_validation->run() === FALSE) {
return $this->index();
} else {
$expression = $this->input->post('search');
$data = $this->Static_model->get_static_data();
$data['categories'] = $this->Categories_model->get_categories();
$data['posts'] = $this->Posts_model->search($expression, $limit, $offset);
$data['expression'] = $expression;
$this->load->view('partials/header', $data);
$this->load->view('search');
$this->load->view('partials/footer');
}
}
}
Posts_model 模型具有帖子列表和搜索结果的代码:
public function get_posts($limit, $offset) {
$this->db->order_by('id', 'DESC');
$query = $this->db->get('posts', $limit, $offset);
return $query->result();
}
public function search_count($expression) {
$query = $this->db->like('title', $expression)
->or_like('description', $expression)
->or_like('content', $expression);
$query = $this->db->get('posts');
return $query->num_rows();
}
public function search($expression, $limit, $offset) {
$query = $this->db->like('title', $expression)
->or_like('description', $expression)
->or_like('content', $expression);
$this->db->order_by('posts.id', 'DESC');
$query = $this->db->get('posts', $limit, $offset);
return $query->result();
}
明确地说,我想避免将其复制到search()
方法中:
To be clear, I want to avoid copying this into the search()
method:
$this->load->library('pagination');
$config['query_string_segment'] = 'page';
$config['total_rows'] = $this->Posts_model->get_num_rows();
$config['per_page'] = 12;
if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
$_GET[$config['query_string_segment']] = 1;
}
$limit = $config['per_page'];
$offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
$this->pagination->initialize($config);
我试图借用分页从index()
到search()
.
缺少什么?
推荐答案
试试这个:
- 在控制器中创建一个私有方法并在此处移动分页器初始化代码
- 在 index()
和 search()
class Posts extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Static_model');
$this->load->model('Posts_model');
$this->load->model('Categories_model');
$this->load->model('Comments_model');
}
private function _initPagination($path, $totalRows) {
//load and configure pagination
$this->load->library('pagination');
$config['base_url'] = base_url($path);
$config['query_string_segment'] = 'page';
//pass $totalRows param to pagination config
$config['total_rows'] = $totalRows;
$config['per_page'] = 12;
if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
$_GET[$config['query_string_segment']] = 1;
}
$this->pagination->initialize($config);
$limit = $config['per_page'];
$offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
return ['limit' => $limit, 'offset' => $offset];
}
public function index() {
//call initialization method
$config = $this->_initPagination("/posts", $this->Posts_model->get_num_rows());
$data = $this->Static_model->get_static_data();
$data['categories'] = $this->Categories_model->get_categories();
//use limit and offset returned by _initPaginator method
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
$this->load->view('partials/header', $data);
$this->load->view('posts');
$this->load->view('partials/footer');
}
public function search() {
$this->form_validation->set_rules('search', 'Search term', 'required|trim|min_length[3]');
$this->form_validation->set_error_delimiters('<p class = "error search-error"> ', ' </p>
');
// If search fails
if ($this->form_validation->run() === FALSE) {
return $this->index();
} else {
$expression = $this->input->post('search');
//call initialization method
$config = $this->_initPagination("/search", $this->Posts_model->search_count($expression));
$data = $this->Static_model->get_static_data();
$data['categories'] = $this->Categories_model->get_categories();
//use limit and offset returned by _initPaginator method
$data['posts'] = $this->Posts_model->search($expression, $config['limit'], $config['offset']);
$data['expression'] = $expression;
$this->load->view('partials/header', $data);
$this->load->view('search');
$this->load->view('partials/footer');
}
}
}
这篇关于Codeigniter 3 博客:如何在 search() 方法中使用 index() 方法的分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Codeigniter 3 博客:如何在 search() 方法中使用 index() 方法的分页?
基础教程推荐
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01