Salman
Salman

Reputation: 3207

Codeigniter Pagination issue

I am trying to get the pagination to work with codeigniter. But am getting the following errors

Severity: Notice Message: Undefined offset: 0 Filename: controllers/group.php

And

Severity: Warning Message: Invalid argument supplied for foreach() Filename: 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. Please note i am new to PHP to be gentel :)

Upvotes: 0

Views: 552

Answers (2)

Damien Pirsy
Damien Pirsy

Reputation: 25445

Here:

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

You're assing the query resultset to $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']. So, either you assing to $query in the model, or you iterate the correct array in the view (like I showed you above)

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

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

Upvotes: 1

Related Questions