每页的记录允许用户选择 - codeigniter分页 [英] records per page allow user to choose - codeigniter pagination
问题描述
我的分页工作。我已经设置了每页5条记录的限制,但我希望用户能够改变,如果他想要的。问题是我不知道如何做。
在视图中,我创建了下拉菜单,所以用户可以选择他想看到每页的记录数:
< ul class =dropdown-menu>
< li>
< a href =<?php echo base_url();?> backOfficeUsers / displayAllUsersid =2class =pPagedata-tableid =smpl_tbl>
每页2条记录
< / a>
< / li>
< li>
< a href =<?php echo base_url();?> backOfficeUsers / displayAllUsersid =50class =pPagedata-tableid =smpl_tbl>
每页50条记录
< / a>
< / li>
< li>< a href =<?php echo base_url();? backOfficeUsers / displayAllUsersid =100class =pPagedata-tableid =smpl_tbl>
每页100条记录
< / a>
< / li>
< li>
< a href =<?php echo base_url();?> backOfficeUsers / displayAllUsersid =allclass =pPagedata-tableid =smpl_tbl>
显示所有记录
< / a>
< / li>
< / ul>
在我的控制器中,我有以下代码:
public function displayAllUsers()
{
$ recordsPerPage = 5;
$ limit = $ recordsPerPage;
$ offset = 3;
$ offset = $ this-> uri-> segment(3);
$ this-> db-> limit($ limit,$ offset);
$ data ['users'] = $ this-> backOfficeUsersModel-> get();
$ totalresults = $ this-> db-> get('back_office_users') - > num_rows();
//初始化&配置分页
$ this-> load-> library('pagination');
$ config ['base_url'] = site_url('/ backOfficeUsers / displayAllUsers');
$ config ['total_rows'] = $ totalresults;
$ config ['per_page'] = $ limit;
$ config ['uri_segment'] = 3;
$ config ['full_tag_open'] ='< div class =dataTables_paginate paging_bootstrap pagination>< ul>';
$ config ['full_tag_close'] ='< / ul>< / div>';
$ config ['cur_tag_open'] ='< li>< a href =#style =color:#ffffff; background-color:#258BB5;>';
$ config ['cur_tag_close'] ='< / a>< / li>';
$ config ['num_tag_open'] ='< li>';
$ config ['num_tag_close'] ='< / li>';
$ this-> pagination-> initialize($ config);
$ data ['main_content'] ='bousers / users';
$ data ['title'] ='后台用户';
$ errorMessage = FALSE;
$ this-> load-> vars($ data,$ errorMessage);
$ this-> load-> vars($ currentUser);
$ this-> load-> view('backOffice / template');
} //函数结束displayAllUsers
告诉我如何显示用户从下拉菜单中选择的记录数?如果他没有选择任何东西,我想默认显示5条记录。
任何帮助将深深赞赏。
尊敬,Zoran
code> echo form_open('controller / displayAllUsers');
$ options = array(
''=>'select',
'2'=>'2',
'50'=>'50'
'100'=>'100');
echo form_dropdown('sel',$ options,'');
echo form_submit('submit',Submit);
控制器
if(isset($ post ['sel'])&&!empty($ post ['sel']))
$ config ['per_page'] = $ post ['sel'];
else
$ config ['per_page'] = 5;
并且在执行此操作之前不要忘记加载形式助手或者你可以给正统的html 选择
框
编辑:
( / em>)
查看:
< ul class = dropdown-menu>
< li>
< a href =<?php echo base_url();?> backOfficeUsers / displayAllUsers / 2class =pPagedata-tableid =smpl_tbl>
每页2条记录
< / a>
< / li>
< li>
< a href =<?php echo base_url();?> backOfficeUsers / displayAllUsers / 50class =pPagedata-tableid =smpl_tbl>
每页50条记录
< / a>
< / li>
< li>< a href =<?php echo base_url();? backOfficeUsers / displayAllUsers / 100class =pPagedata-tableid =smpl_tbl>
每页100条记录
< / a>
< / li>
< li>
< a href =<?php echo base_url();?> backOfficeUsers / displayAllUsersid =allclass =pPagedata-tableid =smpl_tbl>
显示所有记录
< / a>
< / li>
public function displayAllUsers()
{
$ currentUser = $ this-> isLoggedIn();
$ this-> load-> model('backOfficeUsersModel');
$ this-> db-> order_by('userid');
// $ recordsPerPage = 5;
// $ limit = $ recordsPerPage;
if($ this-> uri-> segment(3)!=){
$ limit = $ this-> uri-> segment(3);
} else {
$ limit = 5;
}
$ offset = 4;
$ offset = $ this-> uri-> segment(4);
$ this-> db-> limit($ limit,$ offset);
$ data ['users'] = $ this-> backOfficeUsersModel-> get();
I have pagination that works. I have set the limit to 5 records per page, but I will like the user to be able to change that if he want. Problem is i have no idea how to do it.
In the view, I created drop down menu, so user can choose how many records he want to see per page:
<ul class="dropdown-menu">
<li>
<a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers" id="2" class="pPage" data-tableid="smpl_tbl">
2 records per page
</a>
</li>
<li>
<a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers" id ="50" class="pPage" data-tableid="smpl_tbl">
50 records per page
</a>
</li>
<li><a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers" id="100" class="pPage" data-tableid="smpl_tbl">
100 records per page
</a>
</li>
<li>
<a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers" id="all" class="pPage" data-tableid="smpl_tbl">
Display all records
</a>
</li>
</ul>
In my controller, I have the following code:
public function displayAllUsers()
{
$recordsPerPage = 5;
$limit = $recordsPerPage;
$offset = 3;
$offset = $this->uri->segment(3);
$this->db->limit($limit, $offset);
$data['users'] = $this->backOfficeUsersModel->get();
$totalresults = $this->db->get('back_office_users')->num_rows();
//initializing & configuring paging
$this->load->library('pagination');
$config['base_url'] = site_url('/backOfficeUsers/displayAllUsers');
$config['total_rows'] = $totalresults;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<div class="dataTables_paginate paging_bootstrap pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$config['cur_tag_open'] = '<li><a href=# style="color:#ffffff; background-color:#258BB5;">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['main_content'] = 'bousers/users';
$data['title'] = 'Back Office Users';
$errorMessage = FALSE;
$this->load->vars($data,$errorMessage);
$this->load->vars($currentUser);
$this->load->view('backOffice/template');
} // end of function displayAllUsers
Can anyone tell me how can i display number of records that user selected from the drop down menu? If he doesn't select anything, I would like to display 5 records by default.
Any help will be deeply appreciated.
Regards,Zoran
You shouldn't make a drop down like that.. Try this instead
View
echo form_open('controller/displayAllUsers');
$options = array(
'' => 'Select',
'2' => '2',
'50' => '50',
'100' => '100');
echo form_dropdown('sel',$options,'');
echo form_submit('submit',Submit);
Controller
if(isset($post['sel']) && !empty($post['sel']))
$config['per_page'] = $post['sel'];
else
$config['per_page'] = 5;
And don't forget to load form helper before doing this.. Or else you can give orthodox html select
box
EDIT: (Another way)
View:
<ul class="dropdown-menu">
<li>
<a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers/2" class="pPage" data-tableid="smpl_tbl">
2 records per page
</a>
</li>
<li>
<a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers/50" class="pPage" data-tableid="smpl_tbl">
50 records per page
</a>
</li>
<li><a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers/100" class="pPage" data-tableid="smpl_tbl">
100 records per page
</a>
</li>
<li>
<a href="<?php echo base_url(); ?>backOfficeUsers/displayAllUsers" id="all" class="pPage" data-tableid="smpl_tbl">
Display all records
</a>
</li>
Controller:
public function displayAllUsers()
{
$currentUser = $this->isLoggedIn();
$this->load->model('backOfficeUsersModel');
$this->db->order_by('userid');
//$recordsPerPage = 5;
//$limit = $recordsPerPage;
if ($this->uri->segment(3) !="") {
$limit = $this->uri->segment(3);
} else {
$limit = 5;
}
$offset = 4;
$offset = $this->uri->segment(4);
$this->db->limit($limit, $offset);
$data['users'] = $this->backOfficeUsersModel->get();
这篇关于每页的记录允许用户选择 - codeigniter分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!