user5456337
user5456337

Reputation:

Codeigniter pagination doesn't show page relevant data, but the whole data set

Here is my controller :

function index()
{
    $data['title']="Post";
    $this->load->library('pagination');
    $config['base_url'] = base_url().'Post/index';
    $config['total_rows'] = $this->post_model->get_total_count();
    $config['per_page'] = 1;
    $config['uri_segment'] = 1;
    $this->pagination->initialize($config);
    $data['posts'] = $this->post_model->get_single_post($config['per_page'],$this->uri->segment(1));

    $this->load->view('frontend/layouts/header',$data);
    $this->load->view('frontend/view_post',$data);
    $this->load->view('frontend/layouts/footer');
}

Model

function get_single_post(){
$query= $this->db->select('*')
    ->from('tbl_post,tbl_users')
    ->where('tbl_post.post_created=tbl_users.user_id')
    ->where('tbl_post.post_status',1)
    ->order_by('tbl_post.post_id','asc')
    ->get();
    $data = $query->result_array();
    return $data;
    }



function get_total_count(){
    $this->db->select('*');
    $this->db->from('tbl_post');
    $query = $this->db->get();
    return $query->num_rows();
    }

The relevant page data doesn't show. It shows all of the data in one page!

Upvotes: 0

Views: 320

Answers (2)

Shaiful Islam
Shaiful Islam

Reputation: 7134

There are lots of problem.

  1. $config['uri_segment'] cannot be 1. It should be 3 So fix it to

    $config['uri_segment']=3;

segment 1 means controller name. 2 means function name. 3 means page number. if you use 1 it will replace the controller name with page number.

  1. You passing parameter to your model function but your model not receiving the parameters.So your function should be like this

    function get_single_post($limit,$page_no){

  2. Your model returning all records not the current page records. So add limit

    ->limit($page_no,$limit)//remember page_no should be 0 for 1,1 for 2.

Upvotes: 0

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

$config['uri_segment'] = 1 are you sure on this ?? Check this CI example

As you mentioned $config['base_url'] = base_url().'Post/index'; in question $config['uri_segment'] = 1 should be $config['uri_segment'] = 2


As well doo this too

In Controller

$config['uri_segment'] = 1;
$this->pagination->initialize($config);

$data['links'] = $this->pagination->create_links();
$data['posts'] = $this->post_model->get_single_post($config['per_page'],$this->uri->segment(1));

In View

echo $links;

Edit 01

In Model add this

function get_single_post($page,$limit) # Changed
{
    $query= $this->db->select('*')
        ->from('tbl_post,tbl_users')
        ->where('tbl_post.post_created=tbl_users.user_id')
        ->where('tbl_post.post_status',1)
        ->limit($page,$limit) # Changed
        ->order_by('tbl_post.post_id','asc')
        ->get();
    $data = $query->result_array();
    return $data;
}

Upvotes: 1

Related Questions