如果数据小于6或等等,如何禁用分页? (笨)

时间:2015-09-09 02:32:41

标签: php codeigniter pagination

如果数据小于6,我想禁用分页, 这是我在控制器上的脚本:

 function index(){

    $per_page= $this->input->get('per_page');
    $data['brand'] = $this->brand_model->brand();
    $data['type'] = $this->type_model->type();
    $data['city'] = $this->city_model->city();
    $data['location'] = $this->location_model->location();
    $data['history']= $this->history_transaksi_model->history($to,$per_page);

    $pagination['page_query_string'] = TRUE;
    $pagination['base_url'] = site_url().'history/?';
    $pagination['total_rows'] = 10;
    $pagination['per_page'] = 5;
    $pagination['uri_segment'] = 6;
    $pagination['num_links'] = 3;


    $pagination['full_tag_open'] = '<ul class="pagination">';
    $pagination['full_tag_close'] = '</ul>';
    $pagination['first_link'] = '<<';
    $pagination['first_tag_open'] = '<li class="prev page">';
    $pagination['first_tag_close'] = '</li>';
    $pagination['last_link'] = '>>';
    $pagination['last_tag_open'] = '<li class="next page">';
    $pagination['last_tag_close'] = '</li>';
    $pagination['next_link'] = '>';
    $pagination['next_tag_open'] = '<li class="next page">';
    $pagination['next_tag_close'] = '</li>';
    $pagination['prev_link'] = '<';
    $pagination['prev_tag_open'] = '<li class="prev page">';
    $pagination['prev_tag_close'] = '</li>';
    $pagination['cur_tag_open'] = '<li class="active"><a href="">';
    $pagination['cur_tag_close'] = '</a></li>';
    $pagination['num_tag_open'] = '<li class="page">';
    $pagination['num_tag_close'] = '</li>';


    $this->pagination->initialize($pagination);
    $this->load->view('history.php',$data);
}

在我看来,我使用的是If语句,但是当数据超过5时,分页没有显示,第六个或更多的数据也没有出现。

<?php 

if(count($history->result())==0 || count($history->result()) <=5  ){

}else{
echo $this->pagination->create_links();
}
?>

1 个答案:

答案 0 :(得分:0)

你在寻找什么&amp;你在做什么是错的

你在做什么

$data['history']= $this->history_transaksi_model->history($to,$per_page);//Here actually you are getting data for the page e.g page 3 data
  

这里你应该计算总行数,例如你有100行数据

$pagination['total_rows'] = 10;
  

这应该是从您的模型或对您的表的方法调用   直接db调用表来获取此记录的总记录数   分页

$pagination['total_rows'] = $this->history_transaksi_model->get_total_rows();//if you have any search params pass it to the method

另外我的建议是,虽然您创建的分页数少于6条。

$data['pagination']= '';//init pagination value

if (($pagination['total_rows'] = $this->history_transaksi_model->get_total_rows()) > 5) {
    $pagination['page_query_string'] = TRUE;
    $pagination['base_url'] = site_url().'history/?';
    $pagination['total_rows'] = 10;
    $pagination['per_page'] = 5;
    $pagination['uri_segment'] = 6;
    $pagination['num_links'] = 3;


    $pagination['full_tag_open'] = '<ul class="pagination">';
    $pagination['full_tag_close'] = '</ul>';
    $pagination['first_link'] = '<<';
    $pagination['first_tag_open'] = '<li class="prev page">';
    $pagination['first_tag_close'] = '</li>';
    $pagination['last_link'] = '>>';
    $pagination['last_tag_open'] = '<li class="next page">';
    $pagination['last_tag_close'] = '</li>';
    $pagination['next_link'] = '>';
    $pagination['next_tag_open'] = '<li class="next page">';
    $pagination['next_tag_close'] = '</li>';
    $pagination['prev_link'] = '<';
    $pagination['prev_tag_open'] = '<li class="prev page">';
    $pagination['prev_tag_close'] = '</li>';
    $pagination['cur_tag_open'] = '<li class="active"><a href="">';
    $pagination['cur_tag_close'] = '</a></li>';
    $pagination['num_tag_open'] = '<li class="page">';
    $pagination['num_tag_close'] = '</li>';


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

    $data['pagination'] = $this->pagination->create_links();

}

如果您将自定义 HTML 包裹在分页

if ( ! empty($pagination) ) {
    echo '<div class="pagination">' . $pagination . '</div>';
}

其他只是 echo

echo $pagination;