将一个函数调用到同一模型类的codeigniter中的另一个函数不起作用

时间:2013-12-06 06:07:58

标签: codeigniter

class login_model extends CI_Model 
{
    public function page_data()
    {
        $config['per_page'] = 5;
        $config['uri_segment'] = 3;
        $config['num_links'] = 3;
        $config['full_tag_open'] = '<div class="pagination"><ul>';
        $config['full_tag_close'] = '</ul></div><!--pagination-->';

        $this->pagination->initialize($config);
    }

    public function page()
    {
        $this->page_data();
        $records = $this->db->get('payments',$config['per_page'], $this->uri->segment(3));
        return $records;
    }
}

通过$ this-&gt; login_model-&gt; page()在视图中调用此页面功能。但我得到的查询结果不是分页数据。

2 个答案:

答案 0 :(得分:3)

尝试返回,而不是initializing那样

public function page_data()
{
    $config['per_page'] = 5;
    $config['uri_segment'] = 3;
    $config['num_links'] = 3;
    $config['full_tag_open'] = '<div class="pagination"><ul>';
    $config['full_tag_close'] = '</ul></div><!--pagination-->';

    return $config;
 }

page函数中初始化pagination类似

public function page() {
    $config = $this->page_data();
    $this->pagination->initialize($config);    // Initialize here
    $records = $this->db->get('payments',$config['per_page'], $this->uri->segment(3));
    return $records;
}

答案 1 :(得分:0)

我认为你错过了一些配置,例如$config['base_url'], $config['total_rows']

试试这个:

在控制器中

public function getData() {
  // Load user model
    $this->load->model('user');

    $config['base_url'] = base_url() . 'home/homepage';
    $config['per_page'] = '3';
    $config['total_rows'] = $this->user->getNum();
    $this->pagination->initialize($config);

    $start = 0;
    if ($this->uri->segment(3)) {
        $start = $this->uri->segment(3);
    }

    $data['students'] = $this->user->getStudent($start, $config['per_page']);
    $this->load->view('view', $data);
}

在模型中

// Get students per page
public function getStudent($start, $num){
    $this->db->select('*');
    $this->db->limit($num, $start);
    $data = $this->db->get('students');
    return $data;
}
// Get number of all students to build pagination
public function getNum(){
    $this->db->select('*');
    $data = $this->db->get('students');
    $totalOfRows = $data->num_rows();
    return $totalOfRows;
}

在视图中

<?php
// View data from controller
foreach($students->result() as $row){
    echo $row->name.'<br />';
}

// View pagination
echo $this->pagination->create_links();
?>

注意不要让模型直接与视图进行通信。