Codeigniter分页显示额外的num链接

时间:2015-05-12 10:23:41

标签: codeigniter codeigniter-3

当我在我的类别中搜索页面时,它会显示该类别中页面的结果。

  

为了进行测试,我在 search_for上将每页的限制设置为1   功能

问题/问题:如果由于某种原因只有2页显示在结果中,则分页链接会显示5个链接,其中只显示2个分页链接。

模型的

函数在控制器上进行测试。

我认为问题在于模型中的db-> like和db-> or_like。为什么它只显示5个分页链接,如果只找到2个结果,则只显示2个。

控制器

<?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 as $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 as $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 to 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;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我认为5是所有表格行没有where / like过滤器,它可能来自这个计算所有内容的函数:

{{1}}

在这里你应该添加相同的where子句。

答案 1 :(得分:1)

简单修复感谢@K​​yleK建议我从get_pages_within函数复制了代码,然后返回return $query->num_rows();

所有工作

public function get_total_pages() {
    $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));
    $query = $this->db->get();
    return $query->num_rows();
}