Codeigniter pagination displays extra num links -
when search pages in categories displays result of pages in category.
for testing have set limit per page 1 on search_for function.
question/problem: if there 2 pages show in result reason pagination links display 5 links should display 2 pagination links.
functions model on controller testing.
i think problem lies db->like , db->or_like in model. why display 5 pagination links when should display 2 if 2 results found.
controller
<?php class category_search extends catalog_controller { public function __construct() { parent::__construct(); } public function index() { $data['title'] = "search category"; $data['categories'] = array(); $results = $this->get_categories(); foreach ($results $result) { $data['categories'][] = array( 'category_id' => $result['category_id'], 'name' => $result['name'] ); } $this->load->library('form_validation'); $this->form_validation->set_rules('page', 'category'); $this->form_validation->set_rules('name', 'name'); if ($this->form_validation->run() == false) { $this->load->view('theme/default/template/pages/search_category_view', $data); } else { redirect('pages/category_search/search_for' . '/' . $this->input->post('category')); } } public function search_for($offset = null) { $data['title'] = "caregory results"; $data['heading_title'] = "search" .' - '. $this->get_category_name($this->uri->segment(4)); $data['pages'] = array(); $this->load->library('pagination'); $limit = 1; $config['base_url'] = base_url('pages/category_search/search_for') .'/'. $this->uri->segment(4); $config['total_rows'] = $this->get_total_pages(); $config['per_page'] = $limit; $config['use_page_numbers'] = true; $config['uri_segment'] = 5; $config['num_links'] = "16"; $config['full_tag_open'] = "<ul class='pagination'>"; $config['full_tag_close'] ="</ul>"; $config['num_tag_open'] = '<li>'; $config['num_tag_close'] = '</li>'; $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>"; $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>"; $config['next_tag_open'] = "<li>"; $config['next_tagl_close'] = "</li>"; $config['prev_tag_open'] = "<li>"; $config['prev_tagl_close'] = "</li>"; $config['first_tag_open'] = "<li>"; $config['first_tagl_close'] = "</li>"; $config['last_tag_open'] = "<li>"; $config['last_tagl_close'] = "</li>"; $this->pagination->initialize($config); $page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0; $results = $this->get_pages_within($config['per_page'], $page); $this->load->model('catalog/tool/model_tool_image'); foreach ($results $result) { $data['pages'][] = array( 'category_id' => $result['category_id'], 'parent_id' => $result['parent_id'], 'name' => $result['name'], 'description' => $result['description'], 'image' => $this->model_tool_image->resize($result['image'], 280, 200) ); } $data['pagination'] = $this->pagination->create_links(); $this->load->view('theme/default/template/pages/search_category_search_for_view', $data); } // todo move model functions new model file when complete public function get_categories() { $this->db->select('*'); $this->db->from($this->db->dbprefix . 'category c', 'left'); $this->db->join($this->db->dbprefix . 'category_description cd', 'cd.category_id = c.category_id', 'left'); $query = $this->db->get(); return $query->result_array(); } public function get_pages_within($limit, $offset) { $this->db->select('*'); $this->db->from($this->db->dbprefix . 'page_to_category p2c', 'left'); $this->db->join($this->db->dbprefix . 'page p', 'p.page_id = p2c.page_id', 'left'); $this->db->join($this->db->dbprefix . 'page_description pd', 'pd.page_id = p2c.page_id', 'left'); $this->db->like('p2c.parent_id', (int)$this->uri->segment(4)); $this->db->or_like('p2c.category_id', (int)$this->uri->segment(4)); $this->db->limit($limit, $offset); $query = $this->db->get(); return $query->result_array(); } public function get_total_pages() { return $this->db->count_all($this->db->dbprefix . 'page'); } public function get_category_name($category_id = 0) { $this->db->where('category_id', (int)$category_id); $query = $this->db->get($this->db->dbprefix . 'category_description'); if ($query->num_rows() > 0) { $row = $query->row(); return $row->name; } else { return false; } } }
i suppose 5 table rows no where/like filter , come function count everything:
public function get_total_pages() { return $this->db->count_all($this->db->dbprefix . 'page'); }
here should add same clause.
Comments
Post a Comment