Joseph
Joseph

Reputation: 20

codeigniter pagination page number issue

I'm trying to use Codeigniter pagination library , i want my pagination url to be like this :

mysite.com/codeigniter/controller/page/1

mysite.com/codeigniter/controller/page/2

mysite.com/codeigniter/controller/page/3

here is my controller :

class Controller extends CI_Controller {
    public function index() {
        $this->load->model('model');
        $segment=$this->uri->segment(3);
        /* Pagination */
        $config['base_url']=base_url('controller/index');
        $config['total_rows']=$this->model->count_active_members()->num_rows;
        $config['per_page']=5;
        $config['uri_segment'] = 3;
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        /* END Pagination */
        $result['members']=$this->model->get_active_members($segment);
        $this->load->view('view',$result);
    }
}

my model :

class Model extends CI_Model {
    public function get_active_members($segment) {
        $this->db->select('username,email,balance,ctimes,regdate');
        $this->db->limit(5,$segment);
        return $this->db->get_where('members',array('status'=>'active'));
    }

    public function count_active_members() {
        return $this->db->get_where('members',array('status'=>'active'));
    }   
}

it worked as i want but the problem that i have 9 rows in my db & it displays only 7

Upvotes: 0

Views: 1319

Answers (1)

JC Sama
JC Sama

Reputation: 2204

I think your problem is the offset, try this instead :

$this->db->select('username,email,balance,ctimes,regdate');
$per_page = 5;
$segment = $segment > 0 ? (($segment - 1) * $per_page) : $segment;
$this->db->limit($per_page, $segment);

Upvotes: 2

Related Questions