Php Codeigniter分页问题
我正在尝试使用codeigniter进行分页。但是我得到以下错误 严重性:通知 消息:未定义的偏移量:0 文件名:controllers/group.php 及 严重性:警告 消息:为foreach()提供的参数无效 文件名:views/group\u list\u view.php 下面是我的模型的代码Php Codeigniter分页问题,php,codeigniter,pagination,Php,Codeigniter,Pagination,我正在尝试使用codeigniter进行分页。但是我得到以下错误 严重性:通知 消息:未定义的偏移量:0 文件名:controllers/group.php 及 严重性:警告 消息:为foreach()提供的参数无效 文件名:views/group\u list\u view.php 下面是我的模型的代码 public function get_list($userid) { $this->load->library('pagination'); $conf
public function get_list($userid)
{
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/portal/index.php/group/index';
$config['total_rows'] =$this->db->where('UserId',$userid)->get('group')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$query['groups'] = $this->db->get('group', $config['per_page'], $this->uri->segment(3));
return $query;
}
这是我的控制器的代码
class Group extends CI_Controller {
public function __constructor()
{
session_start();
parent::__constructor();
}
public function index()
{
if(!$this->session->userdata('userid'))
redirect('login');
$this->load->model('group_model');
$result = $this->group_model->get_list($this->session->userdata('$userid'));
//neither this works
data['groups'] = $result;
//nor this
$data['groups'] = $result[0];
$this->load->view('group_list_view', $data);
}
}
这就是视图中的代码
foreach($groups as $group){
echo $group->GroupId;
}
谁能告诉我我在哪里工作。显然,似乎没有其他人遇到过这样的问题。我遵循了同上的教程,但仍然面临同样的问题。请注意,我是新来PHP的gentel:)您需要在控制器而不是模型中编写代码
public function index()
{
if(!$this->session->userdata('userid'))
redirect('login');
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/portal/index.php/group/index';
$config['total_rows'] = // Use the same function to get the number of rows
$config['per_page'] = 10;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$this->load->model('group_model');
$result = $this->group_model->get_list($this->session->userdata('$userid'));
data['groups'] = $result;
$this->load->view('group_list_view', $data);
}
在这里:
$query['groups']=$this->db->get('group',$config['per_page'],$this->uri->segment(3))代码>
您正在将查询结果集分配到$query['group']
但在控制器中,您正在执行
$data['groups'] = $result;
然后迭代:
foreach($groups as $group){
你应该这样做:
foreach($groups['groups'] as $group){
因为您在模型中的原始分配是$query['groups']。
因此,您要么在模型中调用$query
,要么在视图中迭代正确的数组(如我上面所示)请正确检查分页代码是否应该是控制器而不是模型OK。让我将代码移动到控制器,然后再试一次。在控制器中调用分页并不是必需的(尽管“错误”)。另外,你有一个语法错误。你的答案实际上是错误的。因为他甚至没有在控制器中获得$result。那么,需要查看什么呢。可能是第二个问题。如果你使用文本说话,我不明白。请使用所有需要的元音。现在更好:)他在控制器中赋值(在他的一次尝试中),但他在视图中的赋值错误。在加载分页的地方,我认为这不是问题所在。另外,你还有一个语法错误,那是什么?请告诉我,谢谢。起初它对我有帮助,但我仍然无法删除这个bug。然而,当我将代码从模型移动到控制器时,我的代码开始工作。当从模型返回数据时,我仍然无法使其工作:)