user3073170
user3073170

Reputation: 25

call one function to another function in the same model class of codeigniter is not working

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

called this page function in view by $this->login_model->page(). but i got the query result only not the pagination data.

Upvotes: 2

Views: 9106

Answers (2)

Richat CHHAYVANDY
Richat CHHAYVANDY

Reputation: 597

I think you missed some configuration such as $config['base_url'], $config['total_rows']

Try this:

In controller

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

In model

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

In view

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

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

Note Do not make model communicate direct with view.

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

Try to return them instead of initializing there like

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

And in the page function initialize the pagination like

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

Upvotes: 4

Related Questions