Codeigniter的分页无法正常工作

时间:2013-06-06 17:44:36

标签: php codeigniter pagination

我的codeigniter应用程序出了问题。我已经设置了一个像这样的控制器方法:

    public function program_search()
    {
    $config['base_url'] = base_url().'/site/program_search/';
    $config['total_rows'] = $this->db->get('wc_program')->num_rows();
    $config['per_page'] = 15;
    $config['num_links'] = 1;
    $config["uri_segment"] = 3;
    $config['num_links'] = 1;
    $config['full_tag_open'] = '<div class="pagination pagination-small"><ul>';
    $config['full_tag_close'] = '</ul></div>';
    $config['first_link'] = false;
    $config['last_link'] = false;
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = '&laquo;';
    $config['prev_tag_open'] = '<li class="prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_link'] = '&raquo;';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['cur_tag_open'] =  '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li class="page">';
    $config['num_tag_close'] = '</li>';

    $this->pagination->initialize($config);
    $term = $this->input->post('term');

    $this->db->like('JobRef', $term);
    $this->db->or_like('Area', $term);
    $this->db->or_like('Parish', $term);
    $this->db->or_like('WorkType', $term);
    $this->db->or_like('Location', $term);
    $this->db->order_by('wc_program.JobRef');
    $this->db->limit(15, $this->uri->segment(3));  
    $q = $this->db->get('wc_program');
    if($q->num_rows() > 0) {
        $data['results'] = $q->result();    
    }    
    $data["links"] = $this->pagination->create_links();        
    $this->front->set('title', 'Search our current programmes');
    $this->front->set('metadesc', 'Program Search');
    $this->front->set('metakeywords', '');
    $this->front->buffer('content', 'site/program_search_results', $data);
    $this->front->render();
    }

但是它会根据搜索框中输入的内容显示结果,但是如果小于15的结果,它仍会显示分页,这是我不想要的。如果结果超过15,那么再次显示我想要的分页但是然后转到下一页那里什么都没有,从代码可以有人告诉我我做错了什么?

我真的陷入了困境,需要一些帮助......

感谢adavnce

2 个答案:

答案 0 :(得分:0)

你需要移动这个

$this->db->like('JobRef', $term);
$this->db->or_like('Area', $term);
$this->db->or_like('Parish', $term);
$this->db->or_like('WorkType', $term);
$this->db->or_like('Location', $term);
$this->db->order_by('wc_program.JobRef');
$this->db->limit(15, $this->uri->segment(3));  
$q = $this->db->get('wc_program');
if($q->num_rows() > 0) {
    $data['results'] = $q->result();    
}

在您的分页配置之上,以便您可以传递

$q->num_rows()

$config['total_rows']参数..

public function program_search()
{
    $term = $this->input->post('term');

    // records for pagination, without a limit
    $this->db->like('JobRef', $term);
    $this->db->or_like('Area', $term);
    $this->db->or_like('Parish', $term);
    $this->db->or_like('WorkType', $term);
    $this->db->or_like('Location', $term);
    $this->db->order_by('wc_program.JobRef');  
    $q = $this->db->get('wc_program');

    // records for displaying, with a limit
    $this->db->like('JobRef', $term);
    $this->db->or_like('Area', $term);
    $this->db->or_like('Parish', $term);
    $this->db->or_like('WorkType', $term);
    $this->db->or_like('Location', $term);
    $this->db->order_by('wc_program.JobRef');
    $this->db->limit(15, $this->uri->segment(3));  
    $query = $this->db->get('wc_program');

    if($query->num_rows() > 0) {
        $data['results'] = $query->result();    
    }

    // pagination
    $config['base_url'] = base_url().'/site/program_search/';
    $config['total_rows'] = $q->num_rows(); // new pagination setting, using the unlimited results
    $config['per_page'] = 15;
    $config['num_links'] = 1;
    $config["uri_segment"] = 3;
    $config['num_links'] = 1;
    $config['full_tag_open'] = '<div class="pagination pagination-small"><ul>';
    $config['full_tag_close'] = '</ul></div>';
    $config['first_link'] = false;
    $config['last_link'] = false;
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = '&laquo;';
    $config['prev_tag_open'] = '<li class="prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_link'] = '&raquo;';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['cur_tag_open'] =  '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li class="page">';
    $config['num_tag_close'] = '</li>';

    $this->pagination->initialize($config);

    $data["links"] = $this->pagination->create_links();        
    $this->front->set('title', 'Search our current programmes');
    $this->front->set('metadesc', 'Program Search');
    $this->front->set('metakeywords', '');
    $this->front->buffer('content', 'site/program_search_results', $data);
    $this->front->render();
}

您可能也需要调整偏移参数,我无法在本地测试此代码,但它应该让您开始。

答案 1 :(得分:0)

您可以使用$ config ['total_rows']并询问是否有超过15个,如果是,则显示分页链接

if($config['total_rows']>15):
    $data["links"] = $this->pagination->create_links();
endif;