0

I am Working pagination in CodeIgniter. I see tutorials and user manual also. but I can't find the way to do it properly they call the database in Controller for pagination. I want a proper solution.

This is The Controller

    public function index() {

                $this->load->library('pagination');
                $config['base_url'] = 'index.php/Emp';
                $config['total_rows'] = 200;
                $config['per_page'] = 2; 
                $config['uri_segment'] = 3;

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


        $data = array(); 

        $data['showEmployeeTable']=$this->Em->selectEmployeeData(); //FOR SHOWING A EMPLOYEE DATABASE TABLE

        if($query = $this->Em->getDesignationData())  //grabs all record
        {
            $data['records'] = $query;
        }

        if($query = $this->Em->getCityRecord())  //grabs all record
        {
            $data['records_city'] = $query;
        }

        //$this->load->view('emp_update', $data);
        $this->load->view('erv', $data);

    }

This are My models

public function getDesignationData() {

    $query = $this->db->get('emp_desig'); //model created for get data 
    return $query->result();
}


public function getCityRecord() {

    $query = $this->db->get('city'); //model created for get data 
    return $query->result();
}

// ***************** VEDDING PLAN MODELS **********************************************************


public function selectEmployeeData() {  

     $query = $this->db->get('emp_manage');  
     return $query;  
} 

So how can I show Proper Pagination on the View Page? Kindly answer me step by step. I am a newbie in Codeigniter.

And this in the view.

<?php echo $this->pagination->create_links(); ?>
always-a-learner
  • 3,671
  • 10
  • 41
  • 81
  • Please take a look here, hope you will get a solution http://stackoverflow.com/questions/9255850/how-to-configure-pagination-codeigniter?rq=1 and http://stackoverflow.com/questions/23101513/how-to-create-pagination-in-codeiginter?rq=1 – Suvash sarker Jul 24 '15 at 06:30
  • no sir.. it's not helping me they all confusing me i want a simple pagination table. which is completely work and do it properly. if you have a n answer then tell me sir. – always-a-learner Jul 24 '15 at 06:42

2 Answers2

3

Your controllers should be

public function index() {

    $this->load->library('pagination');
    $config = array();
    $config['base_url'] = 'index.php/Emp';
    $config['total_rows'] = 200;
    $config['per_page'] = 2;
    $config['uri_segment'] = 3;
    $this->pagination->initialize($config);


    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data['showEmployeeTable']=$this->Em->selectEmployeeData($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();


    if($query = $this->Em->getDesignationData())  //grabs all record
    {
        $data['records'] = $query;
    }

    if($query = $this->Em->getCityRecord())  //grabs all record
    {
        $data['records_city'] = $query;
    }

      $this->load->view('erv', $data);

}

Your model should be

public function selectEmployeeData($limit, $start) {  

 $this->db->limit($limit, $start);
     $query = $this->db->get('emp_manage');  
     return $query;  
} 

And in your views add following line

<?php echo $links; ?>
Hema Chandra
  • 80
  • 1
  • 11
0

see this code and make some changes as per your requirement:

You controller should be:

    function list($offset=0)
   {
    $num_rows=$this->db->count_all("table_name");
    $config['base_url'] = site_url('list');
    $config['total_rows'] = $num_rows;
    $config['per_page'] = 100;
    $config['num_links'] = round($config['total_rows']/$config['per_page']);
    //$config['use_page_numbers'] = TRUE;
    $config['page_query_string']=TRUE;
    $this->pagination->initialize($config);
    $data["results"] = $this->Em->selectEmployeeData($config["per_page"], $page);
    $this->load->view('viewfile',$data);
   }

Your Model :

function selectEmployeeData($limit,$offset)//fetch all data with pagination
    {
        $data = array();
        $this->db->limit($limit, $offset);
        $Q = $this->db->get('table');
        if($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $data[] = $row;
            }
        }
        $Q->free_result();
        return $data;
    }

In View :

<?php echo $this->pagination->create_links();?>
David Coder
  • 1,138
  • 2
  • 14
  • 48