0

我的 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 则再次显示我想要的分页,但然后转到下一页那里什么都没有,有人可以从代码中告诉我我做错了什么吗?

我真的被困在哪里出了问题,需要一些帮助......

提前感谢

4

2 回答 2

0

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

if($config['total_rows']>15):
    $data["links"] = $this->pagination->create_links();
endif;
于 2013-06-06T18:17:49.500 回答
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();
}

您可能还需要调整偏移参数,我无法在本地测试此代码,但它应该可以帮助您入门。

于 2013-06-06T18:10:11.380 回答