ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php – Codeigniter 3博客:如何在search()方法中使用index()方法的分页?

2019-10-03 06:29:44  阅读:148  来源: 互联网

标签:php codeigniter pagination


我正在使用Codeigniter 3.1.8中的基本博客应用程序.这些帖子是分页的.该应用程序使用pagination.php配置文件.

有一个搜索帖子的搜索框.我希望对搜索结果进行分页,并且由于方法index()和search()在同一个Posts控制器中,我正在寻找一种方法,通过在搜索结果中使用index()内的分页来避免代码冗余.

在帖子控制器我有:

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()方法中:

$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');
      }
    }
  }

标签:php,codeigniter,pagination
来源: https://codeday.me/bug/20191003/1847406.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有