shiv223
shiv223

Reputation: 45

Pagination codeigniter not working

pagination is working but i dont know Why its showing only one data per page?? and i hv 20 data in my db and i can see only 6 data in 6pages :(

This is my controller

function view($page=0){
                $config = array();
                $config["base_url"] = base_url() . "index.php/view_expenses/view";
                $config["total_rows"] = $this->emp_expenses_model->getTotalExpensesCount();
                $config["per_page"] =1;
                $this->pagination->initialize($config);
                $this->data["results"] = $this->emp_expenses_model->getExpenses($config["per_page"], $page);
                $this->data["links"] = $this->pagination->create_links();
                $this->data['title'] = 'Payroll System';
                $this->data['message'] = $this->session->flashdata('message');
                $this->load->view('view_expenses', $this->data);

    }

This is my model

function getTotalExpensesCount() {
            return $this->db->count_all("emp_expenses");
        }
      function getExpenses($limit, $start) {
            $this->db->limit($limit, $start);
            $qry= $this->db->get("emp_expenses");
        return $qry->result();
         }

Any help thanks in advance :D

Upvotes: 1

Views: 9393

Answers (5)

Nil'z
Nil'z

Reputation: 7475

Try this:

function view(){
    $config                 = array();
    $config["base_url"]     = base_url() . "index.php/view_expenses/view/".$this->uri->segment(3);
    $config["total_rows"]   = $this->emp_expenses_model->getTotalExpensesCount();
    $config["per_page"]     = 5;
    $config['uri_segment']  = 3;
    $this->pagination->initialize($config);

    $this->data["results"]  = $this->emp_expenses_model->getExpenses($config["per_page"], $this->uri->segment(3));
    $this->data["links"]    = $this->pagination->create_links();
    $this->data['title']    = 'Payroll System';
    $this->data['message']  = $this->session->flashdata('message');
    $this->load->view('view_expenses', $this->data);

}

function getExpenses($limit, $start = 0) {
    $qry= $this->db->get("emp_expenses", $limit, $start);
    return $qry->result();
}

$config["per_page"] = 5; means that you want to show 5 data per page, $config['uri_segment'] = 3; says what segment will hold the offset which you will be using in your query. $config["total_rows"] defines the total no. of rows in your table. $config["base_url"] defines the url the pagination will hold.
The data you are showing in your view is no way related to the pagination. Check your query and the offset you are getting. E.g. place echo $this->db->last_qeury();die; in function getExpenses() just before return statement.

Upvotes: 1

Ammu
Ammu

Reputation: 138

i have faced this same problem. Now it solved by changing this

$config['uri_segment']

Upvotes: 1

Eswara Reddy
Eswara Reddy

Reputation: 1647

Try this

$config['uri_segment'] = 3;

$config["per_page"] = 3;

Modifiy modal function like this

function getTotalExpensesCount() {
   $this->db->select("count(*) as CNT"); 
   $qry = $this->db->get("emp_expenses");
   $result2 = $qry->row()->CNT;
   return $result2;
}

Upvotes: 1

Prabhukiran
Prabhukiran

Reputation: 149

"dont know Why its showing only one data per page??" because of $config["per_page"] =1;.

Add the following,

$config['uri_segment'] = 3; // change based on ur url

Change the value from $config["per_page"] =1; to $config["per_page"] =3; to display 3 data per page.

Upvotes: 1

GautamD31
GautamD31

Reputation: 28763

You need to pass the uri segment like

$config["uri_segment"] = $last_seg_no;

Here $last_seg_no will be the last segment where you can find the page number.And need to pass the per page param also like

$config["per_page"] = 1;//As per your case

Upvotes: 1

Related Questions