简体   繁体   English

Codeigniter分页问题

[英]Codeigniter Pagination issue

I am trying to get the pagination to work with codeigniter. 我试图让分页与Codeigniter一起使用。 But am getting the following errors 但是出现以下错误

Severity: Notice Message: Undefined offset: 0 Filename: controllers/group.php 严重性:通知消息:未定义偏移量:0文件名:controllers / group.php

And

Severity: Warning Message: Invalid argument supplied for foreach() Filename: views/group_list_view.php 严重性:警告消息:为foreach()提供了无效的参数文件名:views / group_list_view.php

Following is the code of my Model 以下是我的模型的代码

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;

}  

Here is the code for my controller 这是我的控制器的代码

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

And this the code in view 这是代码中的视图

foreach($groups as $group){
                        echo $group->GroupId;
                    }

Can anyone please guide me where am i doing it worng. 谁能指导我我在哪里做。 Apparently no one else seems to have encountered any such problem. 显然,似乎没有其他人遇到过这样的问题。 I followed the tutorial from net.tutsplus ditto but still facing the same issue. 我按照net.tutsplus同上的教程进行操作,但仍然遇到相同的问题。 Please note i am new to PHP to be gentel :) 请注意,我不熟悉PHP是gentel :)

You need to write the code in controller not in model 您需要在控制器中而不是模型中编写代码

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

Here: 这里:

$query['groups'] = $this->db->get('group', $config['per_page'], $this->uri->segment(3));

You're assing the query resultset to $query['group'] 您正在将查询结果集设置为$query['group']

but in the controller you're doing 但是在控制器中

$data['groups'] = $result;

and then you iterate: 然后您迭代:

foreach($groups as $group){

You should be doing instead: 您应该改为:

foreach($groups['groups'] as $group){

Since your original assignment in the model is to $query['groups']. 由于您在模型中的原始分配是到$query['groups']. So, either you assing to $query in the model, or you iterate the correct array in the view (like I showed you above) 因此,要么在模型中使用$query ,要么在视图中迭代正确的数组(就像我上面显示的那样)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM