Php CodeIgniter应用程序3错误:搜索结果分页不正确?

Php CodeIgniter应用程序3错误:搜索结果分页不正确?,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我正在使用CodeIgniter 3.1.8和Bootstrap 4开发 我将应用程序的后端(仪表板)与其前端分离,并使用细枝作为前端视图和添加主题 我认为为后端添加一个搜索功能是个好主意,这样可以方便地浏览自己的帖子(文章) 在myPosts controller(application\controller\dashboard\Posts.php)中,我有: 视图(posts.php): # 标题 出版日期 行动 奇怪的是,虽然所有的帖子都有页码,但是搜索结果 分页不好:例如,当我执行返回

我正在使用CodeIgniter 3.1.8和Bootstrap 4开发

我将应用程序的后端(仪表板)与其前端分离,并使用细枝作为前端视图和添加主题

我认为为后端添加一个搜索功能是个好主意,这样可以方便地浏览自己的帖子(文章)

在myPosts controller
application\controller\dashboard\Posts.php
)中,我有:

视图(posts.php):


#
标题
出版日期
行动
奇怪的是,虽然所有的帖子都有页码,但是搜索结果
分页不好:例如,当我执行返回的搜索时
11结果,即应显示在两页上,分页
显示所有页面,而不是两页

因为您在total_rows配置中使用了所有行计数:
$config['total_rows']=$this->Posts_model->get_num_rows()
在搜索功能中,您需要使用搜索结果的计数:
此结果的行数不带
$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');
    }
} 
公共函数搜索(){
如果(!$this->session->userdata('is\u logged\u in')){
重定向(“登录”);
}
//强制验证,因为表单的方法是GET
$this->form\u validation->set\u data($this->input->get());
$this->form_validation->set_rules('search','search term','required | trim | min|u length[3]',array('min|length'=>'搜索项长度必须至少为3个字符');
$this->form\u validation->set\u error\u分隔符(“

”,”

'); //如果搜索失败 如果($this->form\u validation->run()==FALSE){ 返回$this->index(); }否则{ $expression=$this->input->get('search'); 如果(!$this->session->userdata('is\u logged\u in')){ 重定向(“登录”); } //加载和配置分页 $this->load->library('pagination'); $config['base_url']=base_url(“/dashboard/posts”); $config['query_string_segment']='page'; //下面只换了一行 $config['total\u rows']=$this->Posts\u model->search\u count($expression); $config['per_page']=10; 如果(!isset($\u GET[$config['query\u string\u segment']]))$\u GET[$config['query\u string\u segment']]<1){ $\u GET[$config['query\u string\u segment']=1; } $limit=$config['每页']; $offset=$limit*($this->input->get($config['query\u string\u segment'])-1); $this->pagination->initialize($config); $data=$this->get_data(); $data['expression']=$expression; $data['posts']=$this->posts\u 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'); } }
你能详细说明一下“奇怪的是,搜索结果不是”@TimBrownlaw例如,当我进行搜索时,返回11个结果,这些结果应该显示在两页上,分页显示所有页面而不是两页。
显示所有页面而不是两页
-你的意思是显示所有帖子吗?不是把它们分成两页吗?@Don't我在问题中添加了一个问题的说明。好吧,我想你的意思是显示太多的页面,而不是真正存在的两页?我认为我们实际上需要查看的视图代码部分是
仪表板/部分/分页
,而不是主要的内容视图代码。还有你正在访问的URL,我是指分页参数等。你是说我应该做
$data['posts']=$this->posts\u model->search($expression)
`现在我发送了一个修改请求,
$config['total\u rows']=$this->Posts\u model->search\u count($expression)将其添加到答案中,以便我可以验证它(如果它通过了测试)。是的,你说的很有道理。
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();
}
<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>
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');
    }
}