【问题标题】:Codeigniter pagination issues on controllers控制器上的 Codeigniter 分页问题
【发布时间】:2018-12-14 12:08:24
【问题描述】:

我正在使用 Codeigniter PHP 框架构建一个投票网站。但我目前陷入困境,因为我无法真正弄清楚脚本中的错误所在。

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

这是我的代码:

## 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));

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

$route['members/manage/(:any)'] = 'admin/Naphs_Members/manage/$1';

$route['roles/manage/(:any)'] = 'admin/Naphs_Roles/manage/$1';

视图都运行良好,分页链接显示良好。但是,当点击每个分页的第一个链接(第 1 页)时,会显示 404 页面。

另外,6 条记录的表只显示 4 条记录,4 条记录的表只显示 2 条……以此类推。

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

【问题讨论】:

  • 它不起作用。我收到一个错误:“在布尔值上调用成员函数 result_array”。当我使用 $this->db->get ('table_name') 时没有显示错误,但获取的记录不完整。
  • 请显示你的代码
  • 我已经解决了...请检查我的答案。

标签: php codeigniter pagination routing


【解决方案1】:

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

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

改为:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 2015-06-06
    • 2011-04-04
    相关资源
    最近更新 更多