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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀