控制器上的Codeigniter分页问题

时间:2018-07-05 23:02:51

标签: php codeigniter pagination routing

我正在使用Codeigniter PHP框架构建一个轮询网站。但是我目前很困,因为我无法真正确定脚本中的错误位置。

该网站有一个管理端,在这里我需要对将从数据库表中提取的记录进行制表和分页,因此我创建了一个Model方法来提取记录并对其进行分页。但这似乎有问题,因为即使显示了我的分页链接,某些记录也没有显示。

这是我的代码:

/var/log

这是我在一个控制器上使用上述方法的方式

## this is a general method for ALL data selection operations and pagination ##
public function select_db_data_paginate($table_name, $array_attributes){

//pagination configuration
//configuration details for pagination
$config['base_url'] = base_url().$array_attributes['base_url'];

$config['total_rows'] = $this->db->count_all('naphs_roles');

$config['per_page'] = $array_attributes['per_page'];

$config['uri_segment'] = 3;

$config['display_pages'] = TRUE;

//PAGINATION STYLING
$config['prev_link'] = '❮ Prev';

$config['next_link'] = 'Next ❯';

$config['attributes'] = array('class' => 'w3-button');

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

//where to start fetching DB records
$start_from =  ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

//fetch the records
//$query = $this->db->get($table_name, $config['per_page'], $start_from);

$query = $this->db->get($table_name,$config['per_page'], $start_from);

$result = $query->result_array();

return $result;

}

这是另一种使用上面模型中创建的分页方法的控制器方法:

//fetch results based on the limit and start point. this data will be used to draw a table

$data['naphs_members'] = $this->Naphs_Model->select_db_data_paginate('naphs_members',array('base_url' => 'index.php/members/manage', 'per_page' => 2));

//create the pagination links

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

//This data will be used to draw a table
$naphs_members = $this->Naphs_Model->select_db_data_paginate('naphs_members', array('base_url' => 'index.php/members/manage', 'per_page' => 2));

最后,这些是我进入页面的路线(方法):

//fetch results based on the limit and start point. this data will be used to draw a table
$data['naphs_roles'] = $this->Naphs_Model->select_db_data_paginate('naphs_roles',array('base_url' => 'index.php/roles/manage', 'per_page' => 2));

//create the pagination links
$data['pagination_links'] = $this->pagination->create_links();

//This data will be used to draw a table
$naphs_roles = $this->Naphs_Model->select_db_data_paginate('naphs_roles',array('base_url' => 'index.php/roles/manage', 'per_page' => 2));

视图均正常运行,分页链接显示良好。但是,当单击每个分页的第一个链接(页面1)时,将显示404页面。

另外,一个包含6条记录的表仅显示4条记录,而包含4条记录的表仅显示2条记录...依此类推。

如果需要,我的屏幕截图将发布在这里。谢谢!

1 个答案:

答案 0 :(得分:0)

已解决!我看到了问题并解决了。在Model方法中,我错误地从“固定”表中获取了总表行,而不是在该方法中使用$ table_name参数。所以选择:

$config['total_rows'] = $this->db->count_all('naphs_roles');

已更改为:

$config['total_rows'] = $this->db->count_all($table_name);

其中$ table_name是Model方法中的第一个参数。谢谢大家!