简体   繁体   English

使用CodeIgniter进行分页

[英]pagination with CodeIgniter

Hi i'm trying to get the pagination right with code igniter but it seems that it doesn't want to work correctly. 嗨,我正在尝试使用代码点火器使分页正确,但似乎它并不想正常工作。 I get the second page, but there the pagination disappears, I still got the right table though and I have 2 errors: 我得到第二页,但是分页消失了,尽管我仍然得到了正确的表格,但有两个错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: models/evaluation_model.php
Line Number: 37

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/evaluation_model.php
Line Number: 37

The controller function: 控制器功能:

function showEvaluations($offset = 0)
        {
            if($this->login->is_logged_in())
            {
                $limit = 5;
                $result = $this->evaluation_model->getAllEvaluations($limit, $offset);

                if ($this->session->userdata('type') == 'admin')
                {
                    $data['evaluations'] = $result['evaluations'];
                    $data['total'] = $result['num_rows'];
                    $data['notallowed'] = false;
                    $config = array();
                    $config['base_url'] = base_url("evaluation/showEvaluations/");
                    $config['total_rows'] = $data['total'];
                    $config['per_page'] = $limit;
                    $config['uri_segment'] = 3;
                    $this->pagination->initialize($config);
                    $data['pagination'] = $this->pagination->create_links();
                    $this->load->view('allevaluations_view',  $data);
                }
                else
                {
                    $data['notallowed'] = true;
                    $this->load->view('allevaluations_view',  $data);
                }


                //$this->load->view('allevaluations_view',  $data);
            }
            else
            {
                $this->load->view('login_view');
            }
        }

and the Model: 和模型:

function getAllEvaluations($limit, $offset)
        {
            $q = $this->db->select('tblPunten.PK_PuntID, tblPunten.Titel, tblPunten.Score, tblVakken.Vak, tblUsers.username, tblUsers.Voornaam, tblUsers.Achternaam')
                          ->from('tblPunten')
                          ->join('tblVakken', 'tblPunten.FK_VakID = tblVakken.PK_VakID')
                          ->join('tblUsers', 'tblPunten.FK_UserID = tblUsers.PK_UserID')
                          ->limit($limit, $offset);

            $query['evaluations'] = $q->get()->result();

            $q = $this->db->select('COUNT(*) as count', FALSE)
                          ->from('tblPunten')
                          ->limit($limit, $offset);
             $tmp = $q->get()->result();
             $query['num_rows'] = $tmp[0]->count;
             return $query;


        }

Line 37: $query['num_rows'] = $tmp[0]->count; 第37行: $query['num_rows'] = $tmp[0]->count;

Found it, the second query: 找到了,第二个查询:

$q = $this->db->select('COUNT(*) as count', FALSE)
                          ->from('tblPunten')
                          ->limit($limit, $offset);

must be without the limit: 必须没有限制:

$q = $this->db->select('COUNT(*) as count', FALSE)
                      ->from('tblPunten')

In codeigniter, ->result() an object. 在codeigniter中, ->result()一个对象。

if you want in that form of array then we can use ->result_array() so that we can get the result set in the form of array. 如果您希望使用数组形式,则可以使用->result_array()以便以数组形式获取结果集。

$tmp = $q->get()->result_array();//made changes
$query['num_rows'] = $tmp[0]['count'];//will work

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

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