0
votes

When I search for a pages in my categories it displays the result of what pages are in that category.

For testing I have set the limit per page to 1 on my search_for function.

Question/Problem: If there a only 2 pages that show up in result for some reason the pagination links display 5 links where it should only display 2 pagination links.

functions for model are on controller for testing.

I think the problem lies where the db->like and db->or_like in model. Why does it display 5 pagination links when should only display 2 if only 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 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

2 Answers

1
votes

I suppose 5 is all the table rows with no where/like filter and it probably come from this function that count everything:

public function get_total_pages() {
    return $this->db->count_all($this->db->dbprefix . 'page');
}

Here you should add the same where clause.

1
votes

Simple fix thanks to @KyleK suggestion I copied the code from the get_pages_within function and then returned return $query->num_rows();

All working

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();
}