dsg24156 2013-11-16 07:53
浏览 46
已采纳

Codeigniter分页问题

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 :)

  • 写回答

2条回答 默认 最新

  • douchen9855 2013-11-16 07:59
    关注

    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);
    }    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog