Codeigniter分页涉及从下拉菜单中选择的行数(动态)

时间:2018-10-03 11:48:50

标签: php codeigniter dynamic row

我在使用CI进行分页时遇到问题。首先,我希望页面显示5个活动记录,并带有下拉按钮以选择要显示的行数(5,10,15,...),并带有分页(索引)。我的问题是,每当我选择(例如)5行时,它将首先显示分页。但是,当我单击第2页(或下一页)时,分页将丢失,我必须选择行数(5),然后再次单击“确定”。有人可以帮我吗?

我的代码:

CONTROLLER

public function viewBranch($offset = 0)
{

    $this->load->helper('url');
    $this->load->database();
    $this->load->library('pagination');
    $this->load->helper('form');
    $this->load->model('insertBranch_model');

    $limit_per_page = $this->input->post('getRows');
    echo $limit_per_page;
    $num_rows=$this->db->count_all("branch");

    $config['base_url'] = base_url().'index.php/main_controller/viewBranch/';
    $config['total_rows'] = $num_rows;
    $config['per_page'] = $limit_per_page;
    $config['uri_segment'] = 3;
    //styling
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="active"><a href="javascript:void(0);">';
    $config['cur_tag_close'] = '</a></li>';
    $config['next_link'] = 'Next';
    $config['prev_link'] = 'Prev';
    $config['next_tag_open'] = '<li class="pg-next">';
    $config['next_tag_close'] = '</li>';
    $config['prev_tag_open'] = '<li class="pg-prev">';
    $config['prev_tag_close'] = '</li>';
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';

    $this->pagination->initialize($config);
    $data['details']=$this->insertBranch_model->viewDetails($offset,$config,$limit_per_page);
    $this->load->view('branchDetails',$data);

}

型号:

public function viewDetails($offset,$config,$limit_per_page){

    $callRow = $this->db->select('*')
                    ->from('branch')
                    ->limit($limit_per_page)
                    ->offset($offset)
                    ->order_by('branchCode', 'asc')
                    ->get();

    return $callRow->result();

}

查看:

Number of rows:

<select name="getRows" id="getRows">
  <option value="">Select</option>
  <option selected="selected" value="5">5</option>
  <option value="10">10</option>
  <option value="20">20</option>
</select>

1 个答案:

答案 0 :(得分:0)

我认为您必须使用那个。

public function viewBranch($offset ="0")
{
    $this->load->helper('url','form');
    $this->load->library('pagination');
    $this->load->model('insertBranch_model');

    if(!empty($this->input->post('getRows')){
     $limit_per_page = $this->input->post('getRows');
    }else{
     $limit_per_page = 5;
    }
    $num_rows = $this->insertBranch_model->getallcount();

    $config['base_url'] = base_url().'index.php/main_controller/viewBranch';
    $config['total_rows'] = $num_rows;
    $config['per_page'] = $limit_per_page;
    $config['uri_segment'] = 3;
    //styling
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="active"><a href="javascript:void(0);">';
    $config['cur_tag_close'] = '</a></li>';
    $config['next_link'] = 'Next';
    $config['prev_link'] = 'Prev';
    $config['next_tag_open'] = '<li class="pg-next">';
    $config['next_tag_close'] = '</li>';
    $config['prev_tag_open'] = '<li class="pg-prev">';
    $config['prev_tag_close'] = '</li>';
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';

    $this->pagination->initialize($config);
    $data['details']=$this->insertBranch_model->viewDetails($offset,$config,$limit_per_page);
    $this->load->view('branchDetails',$data);

}

型号:

public function viewDetails($offset,$config,$limit_per_page){
    $callRow = $this->db->select('*')
                    ->from('branch')
                    ->limit($limit_per_page)
                    ->offset($offset)
                    ->order_by('branchCode', 'asc')
                    ->get();

    return $callRow->result();
}

public function getallcount(){
  return $this->db->count_all_results('branch');
 }

查看:

<select name="getRows" id="getRows">
  <option value="">Select</option>
  <option selected="selected" value="5">5</option>
  <option value="10">10</option>
  <option value="20">20</option>
</select>