Codeigniter 分页和搜索显示每页上的所有记录
我使用的是codeigniter 2.1.4,我试图在一个页面上显示所有用户文件,每页分页5条记录。分页链接可以工作,但每个链接都显示所有记录 我的模型Codeigniter 分页和搜索显示每页上的所有记录,codeigniter,pagination,Codeigniter,Pagination,我使用的是codeigniter 2.1.4,我试图在一个页面上显示所有用户文件,每页分页5条记录。分页链接可以工作,但每个链接都显示所有记录 我的模型 public function get_user_files($user_id, $keyword = NULL) { $select = array( 'files.name as filename', 'files.id, files.remarks', 'boxes.id as
public function get_user_files($user_id, $keyword = NULL) {
$select = array(
'files.name as filename',
'files.id, files.remarks',
'boxes.id as box_id',
'boxes.bin_id as bin_id',
'box_types.name as box_type',
'companies.name as companyname'
);
$from = array('files','boxes','box_types','companies');
$where = array(
'files.status' => 'Warehouse',
'files.box_id' => 'boxes.id',
'boxes.box_type_id' => 'box_types.id',
'boxes.company_id' => 'companies.id',
'boxes.user_id' => $user_id
)
$this->db
->select($select)
->from($from)
->where($where);
if ($keyword) {
$this->db->like('files.name', $keyword);
}
$query = $this->db->get()->result_array();
return $query;
}
我的控制器
public function retrieve_box() {
if ($this->correct_permission('client') == false) {
redirect(base_url());
}
$this->load->helper('form');
$keyword = $this->input->post('keyword');
$data['files'] = $this->File->get_user_files($this->data['user_id'], $keyword);
$total_rows = count($data['files']);
// Pagination
$this->load->library('pagination');
$config['base_url'] = base_url() . 'client/service_request/type/retrieve-box';
$config['uri_segment'] = 5;
$config['total_rows'] = $total_rows;
$config['per_page'] = 5;
$this->pagination->initialize($config);
$data['pages'] = $this->pagination->create_links();
$this->load->view('retrieve_box', $data);
}
我的网址是这样的
www.website.com/client/service_request/type/retrieve-box/
我的看法
<?php
$attributes = array('class' => 'standardForm', 'id' => 'retrieveBoxesForm');
echo form_open('', $attributes);
?>
<fieldset>
<ul>
<li>
<?php
$data_form = array(
'name' => 'keyword',
'id' => 'boxes-field1',
'class' => 'textbox2',
);
echo form_input($data_form);
?>
<?php
$data_form = array(
'class' => 'button2',
'value' => 'Search',
);
echo form_submit($data_form);
?>
</li>
</ul>
</fieldset>
<div class="clear"></div>
<?php echo form_close(); ?>
<div class="standardTable">
<table>
<thead>
<tr>
<th><span class="checkboxReplacement"><input type="checkbox" /></span></th>
<th>Type</th>
<th>Filename</th>
<th>Box ID</th>
<th>Company</th>
<th>Location</th>
<th>Remarks</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($files as $file) { ?>
<tr>
<td><span class="checkboxReplacement"><input type="checkbox" /></span></td>
<td><?php echo $file['box_type']; ?></td>
<td><?php echo $file['filename']; ?></td>
<td><?php echo $file['box_id']; ?></td>
<td><?php echo $file['companyname']; ?></td>
<td><?php echo $file['bin_id']; ?></td>
<td><?php echo $file['remarks']; ?></td>
<td>
<a href="<?php echo base_url('client/retrievebox/' . $file['box_id']); ?>">Retrieve File</a> |
<a href="<?php echo base_url('client/retrievefile/' . $file['id']); ?>">Retrieve Box</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php echo $pages; ?>
</div>
但是当我点击任何分页链接时,我会被带到相应的url,但所有记录都会显示出来
谢谢你阅读这篇文章。真的很难找到解决办法
注意:)$data['pages']=$this->pagination->create_links() 无需通过数据数组传递分页。(上一行) 只需
echo$this->pagination->create_links()代码>
行
另外,请更改$config['uri_segment']
的值并进行检查
更新
好的,问题是您没有向查询添加限制和偏移量
添加到控制器
public function retrieve_box() {
if ($this->correct_permission('client') == false) {
redirect(base_url());
}
$this->load->helper('form');
$keyword = $this->input->post('keyword');
$total = $this->File->get_user_files($this->data['user_id'], $keyword,'','');
$total_rows = count($total);
// Pagination
$this->load->library('pagination');
$config['base_url'] = base_url() . 'client/service_request/type/retrieve-box';
$config['uri_segment'] = 5;
$config['total_rows'] = $total_rows;
$config['per_page'] = 5;
$this->pagination->initialize($config);
$data['pages'] = $this->pagination->create_links();
$limit=$config['per_page'];
$offset= $this->uri->segment(5);
$data['files'] = $this->File->get_user_files($this->data['user_id'], $keyword,$limit,$offset);
$this->load->view('retrieve_box', $data);
}
型号
public function get_user_files($user_id, $keyword = NULL,$limit='',$offset='') {
if(!empty($limit)){
$this->db->limit($limit);
}
//offset
if(!empty($offset)){
$this->db->offset($offset);
}
$this->db->select('files.name as filename, files.id, files.remarks')->from('files');
$this->db->select('boxes.id as box_id, boxes.bin_id as bin_id')->from('boxes');
$this->db->select('box_types.name as box_type')->from('box_types');
$this->db->select('companies.name as companyname')->from('companies');
$this->db->where('files.status', 'Warehouse');
$this->db->where('files.box_id = boxes.id');
$this->db->where('boxes.box_type_id = box_types.id');
$this->db->where('boxes.company_id = companies.id');
$this->db->where('boxes.user_id', $user_id);
if ($keyword) {
$this->db->like('files.name', $keyword);
}
$query = $this->db->get()->result_array();
return $query;
}
您需要为它提供两个函数。首先,计算用户拥有的总文件数,然后按分页方式获取结果。这里的错误在于,您在一个查询中获取所有记录,并循环遍历所有结果
/* count all results */
function count_all_user_files($user_id, $keyword = NULL) {
$this->db->from('files');
$this->db->join('boxes', 'files.box_id = boxes.id');
$this->db->join('box_types', 'boxes.box_type_id = box_types.id');
$this->db->join('companies', 'boxes.company_id = companies.id');
$this->db->where('files.status', 'Warehouse');
$this->db->where('files.box_id = boxes.id');
$this->db->where('boxes.box_type_id = box_types.id');
$this->db->where('boxes.company_id = companies.id');
$this->db->where('boxes.user_id', $user_id);
if ($keyword) {
$this->db->like('files.name', $keyword);
}
return $this->db->count_all_results();
}
/* get result to show */
function all_user_files( $user_id, $keyword = NULL, $limit, $offset ) {
$this->db->select('files.name as filename, files.id, files.remarks');
$this->db->select('boxes.id as box_id, boxes.bin_id as bin_id');
$this->db->select('box_types.name as box_type');
$this->db->select('companies.name as companyname');
$this->db->from('files');
$this->db->join('boxes', 'files.box_id = boxes.id');
$this->db->join('box_types', 'boxes.box_type_id = box_types.id');
$this->db->join('companies', 'boxes.company_id = companies.id');
$this->db->where('files.status', 'Warehouse');
$this->db->where('files.box_id = boxes.id');
$this->db->where('boxes.box_type_id = box_types.id');
$this->db->where('boxes.company_id = companies.id');
$this->db->where('boxes.user_id', $user_id);
if ($keyword) {
$this->db->like('files.name', $keyword);
}
return $this->db->get()->result_array();
}
/* Config */
$config['base_url'] = base_url() . 'client/service_request/type/retrieve-box';
$config['uri_segment'] = 5;
$config['total_rows'] = $this->model_name->count_all_user_files( $user_id, $keyword );
$config['per_page'] = 5;
$data['files'] = $this->File->get_user_files($this->data['user_id'], $keyword, $config['per_page'], $this->uri->segment(5));
select QUERY中的限制在哪里$total_行中的值是多少?我是否回显$this->pagination->create_links();在控制器中的视图?回显中。我没有显示分页的问题。只是每页都显示所有行。在我的案例中有12条记录。因此,www.website.com/client/service_request/type/retrieve-box/www.website.com/client/service_request/type/retrieve-box/5 www.website.com/client/service_request/type/retrieve-box/10所有显示12条记录扫描您请将视图添加到您的问题谢谢您,阿鲁努,我现在已经添加了viewWorks。有一个新的错误使得分页添加了一个额外的链接。也设法解决了这个问题
/* count all results */
function count_all_user_files($user_id, $keyword = NULL) {
$this->db->from('files');
$this->db->join('boxes', 'files.box_id = boxes.id');
$this->db->join('box_types', 'boxes.box_type_id = box_types.id');
$this->db->join('companies', 'boxes.company_id = companies.id');
$this->db->where('files.status', 'Warehouse');
$this->db->where('files.box_id = boxes.id');
$this->db->where('boxes.box_type_id = box_types.id');
$this->db->where('boxes.company_id = companies.id');
$this->db->where('boxes.user_id', $user_id);
if ($keyword) {
$this->db->like('files.name', $keyword);
}
return $this->db->count_all_results();
}
/* get result to show */
function all_user_files( $user_id, $keyword = NULL, $limit, $offset ) {
$this->db->select('files.name as filename, files.id, files.remarks');
$this->db->select('boxes.id as box_id, boxes.bin_id as bin_id');
$this->db->select('box_types.name as box_type');
$this->db->select('companies.name as companyname');
$this->db->from('files');
$this->db->join('boxes', 'files.box_id = boxes.id');
$this->db->join('box_types', 'boxes.box_type_id = box_types.id');
$this->db->join('companies', 'boxes.company_id = companies.id');
$this->db->where('files.status', 'Warehouse');
$this->db->where('files.box_id = boxes.id');
$this->db->where('boxes.box_type_id = box_types.id');
$this->db->where('boxes.company_id = companies.id');
$this->db->where('boxes.user_id', $user_id);
if ($keyword) {
$this->db->like('files.name', $keyword);
}
return $this->db->get()->result_array();
}
/* Config */
$config['base_url'] = base_url() . 'client/service_request/type/retrieve-box';
$config['uri_segment'] = 5;
$config['total_rows'] = $this->model_name->count_all_user_files( $user_id, $keyword );
$config['per_page'] = 5;
$data['files'] = $this->File->get_user_files($this->data['user_id'], $keyword, $config['per_page'], $this->uri->segment(5));