php - CodeIgniter 应用程序 3 错误:分页无法正确呈现搜索结果?
问题描述
我正在使用CodeIgniter 3.1.8和 Bootstrap 4开发在线报纸/博客应用程序。
我已将应用程序的后端(仪表板)与其前端分开,并将 Twig 用于前端视图并添加主题。
我认为为后端添加搜索功能是一个好主意,以便于浏览自己的帖子(文章)。
在我的帖子控制器(application\controllers\dashboard\Posts.php
)中,我有:
private function get_data(){
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['number_of_pages'] = $this->Pages_model->count_pages();
$data['number_of_posts'] = $this->Posts_model->get_num_rows();
$data['number_of_categories'] = $this->Categories_model->get_num_rows();
$data['number_of_comments'] = $this->Comments_model->get_num_rows();
return $data;
}
public function index() {
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
//load and configure pagination
$this->load->library('pagination');
$config['base_url'] = base_url("/dashboard/posts");
$config['query_string_segment'] = 'page';
$config['total_rows'] = $this->Posts_model->get_num_rows();
$config['per_page'] = 10;
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->get_data();
$data['posts'] = $this->Posts_model->get_posts($limit, $offset);
$data['offset'] = $offset;
$this->load->view('dashboard/partials/header', $data);
$this->load->view('dashboard/posts');
$this->load->view('dashboard/partials/footer');
}
public function search() {
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
// Force validation since the form's method is GET
$this->form_validation->set_data($this->input->get());
$this->form_validation->set_rules('search', 'Search term', 'required|trim|min_length[3]',array('min_length' => 'The Search term must be at least 3 characters long.'));
$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->get('search');
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
//load and configure pagination
$this->load->library('pagination');
$config['base_url'] = base_url("/dashboard/posts");
$config['query_string_segment'] = 'page';
$config['total_rows'] = $this->Posts_model->get_num_rows();
$config['per_page'] = 10;
if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
$_GET[$config['query_string_segment']] = 1;
}
$limit = $config['per_page'];
$offset = $limit * ($this->input->get($config['query_string_segment']) - 1);
$this->pagination->initialize($config);
$data = $this->get_data();
$data['expression'] = $expression;
$data['posts'] = $this->Posts_model->search($expression, $limit, $offset);
$data['offset'] = $offset;
$this->load->view('dashboard/partials/header', $data);
$this->load->view('dashboard/posts');
$this->load->view('dashboard/partials/footer');
}
}
在Posts_model模型中,我有这个帖子列表:
public function get_posts($limit, $offset) {
$this->db->select('posts.*,categories.name as post_category');
$this->db->order_by('posts.id', 'DESC');
$this->db->join('categories', 'posts.cat_id = categories.id', 'inner');
$query = $this->db->get('posts', $limit, $offset);
return $query->result();
}
视图(posts.php):
<div class="card-body bg-white p-0">
<div class="table-responsive">
<table class="table table-striped table-sm mb-0">
<thead>
<tr>
<th class="text-right">#</th>
<th class="w-50">Title</th>
<th>Publication date</th>
<th class="text-right pr-2">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $index => $post): ?>
<tr data-slug="<?php echo $post->slug; ?>">
<td class="text-right"><?php $count = $index + 1; echo $count + $offset; ?></td>
<td><?php echo $post->title; ?></td>
<td><?php echo nice_date($post->created_at, 'D, M d, Y'); ?></td>
<td class="text-right">
<div class="btn-group btn-group-sm" role="group">
<a href="<?php echo base_url('posts/post/') . $post->slug; ?>" class="btn btn-success"><i class="fa fa-eye"></i> View</a>
<?php if(($this->session->userdata('is_logged_in') && $this->session->userdata('user_id') == $post->author_id) || $this->session->userdata('user_is_admin')) : ?>
<a href="<?php echo base_url('dashboard/posts/edit/') . $post->slug; ?>" class="btn btn-success"><i class="fa fa-pencil-square-o"></i> Edit</a>
<a href="#" data-slug="<?php echo $post->slug ?>" class="delete-post ajax-btn btn btn-success"><i class="fa fa-trash"></i> Delete</a>
<?php else: ?>
<a href="#" class="btn btn-success disabled"><i class="fa fa-pencil-square-o"></i> Edit</a>
<a href="#" class="btn btn-success disabled"><i class="fa fa-trash"></i> Delete</a>
<?php endif; ?>
</div>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
<div class="card-footer bg-white py-1">
<?php $this->load->view("dashboard/partials/pagination");?>
</div>
</div>
虽然所有帖子的分页都很好,但奇怪的是,搜索结果没有很好地分页:例如,当我进行搜索返回 11 个结果时,应该显示在两个页面上,分页显示所有页面而不是两个页面。
我一直无法弄清楚为什么。
我的错误在哪里?
解决方案
虽然所有帖子的分页都很好,但奇怪的是,搜索结果没有很好地分页:例如,当我进行搜索返回 11 个结果时,应该显示在两个页面上,分页显示所有页面而不是两个页面。
因为您在 total_rows 配置中使用了所有行数:
$config['total_rows'] = $this->Posts_model->get_num_rows();
在搜索功能中,您需要使用搜索结果的计数:此结果中的行数WITHOUT !!! $limit
, 和$offset
:
public function search() {
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
// Force validation since the form's method is GET
$this->form_validation->set_data($this->input->get());
$this->form_validation->set_rules('search', 'Search term', 'required|trim|min_length[3]',array('min_length' => 'The Search term must be at least 3 characters long.'));
$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->get('search');
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
//load and configure pagination
$this->load->library('pagination');
$config['base_url'] = base_url("/dashboard/posts");
$config['query_string_segment'] = 'page';
// Only one line changed below
$config['total_rows'] = $this->Posts_model->search_count($expression);
$config['per_page'] = 10;
if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
$_GET[$config['query_string_segment']] = 1;
}
$limit = $config['per_page'];
$offset = $limit * ($this->input->get($config['query_string_segment']) - 1);
$this->pagination->initialize($config);
$data = $this->get_data();
$data['expression'] = $expression;
$data['posts'] = $this->Posts_model->search($expression, $limit, $offset);
$data['offset'] = $offset;
$this->load->view('dashboard/partials/header', $data);
$this->load->view('dashboard/posts');
$this->load->view('dashboard/partials/footer');
}
}
推荐阅读
- perl - deploying xs module on CPAN
- c# - Do non-existing private variables make python less secure than C#?
- javascript - 如何在javascript中找出第一个孩子的prev()?
- python - eApprox 与 while 循环
- css - 3列布局的高度问题(溢出)
- python - 如何在 Pandas 的同一列中将每个值与项目的最大值进行比较
- vb.net - 如何计算内部收益率(IRR)?
- reactjs - React 导入部分大括号的含义 | 还原
- angular - util方法的静态字段或const函数?
- equality - 为什么J公理在给出x,y的签名时需要2 x?