Harshal Mahajan Harshal Mahajan - 3 months ago 13
PHP Question

Pagination not working properly in codeigniter

I have pagination on my view which is working fine if i am clicking on next number link.like:

1 2 >


So if i am clicking on 2 it goes on second page and shows proper result.but now if i want to come back on 1 than its not redirecting me. Even if there is link 3 than i couldn't come on 2.

Here is my code:

Controller:

function list_providers()
{

$admin_session_data= $this->session->userdata('user_logged_in');

$this->load->model('/admin/provider_model');
$this->load->library('pagination');
$this->load->helper('url');

$config['base_url'] = base_url()."index.php/admin/provider/list_providers/";
$config['total_rows'] = $this->provider_model->countprovider();
$config['per_page'] = 3;
$config['creat_link'] = 1;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';

$this->pagination->initialize($config);
$offset=($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data['list']= $this->provider_model->listproviders($config['per_page'],$offset);

//$data['customer']= $this->provider_model->getcustomer_data($config['per_page'],$this->uri->segment(3));


if(!empty($data))
{
$this->load->view('admin/admin_header',$admin_session_data);
$this->load->view('admin/admin_top_nav',$admin_session_data);
$this->load->view('admin/admin_left',$admin_session_data);
$this->load->view('admin/provider_list',$data);
$this->load->view('admin/admin_footer',$admin_session_data);
}

}


Model:

function listproviders($num,$offset)
{
$this->load->library('pagination');

//$query=$this->db->get_where('users', array('user_role_id'=>'3'));
$query=$this->db->get('users',$num,$offset);

// return result set as an associative array
return $query->result_array();
}


function countprovider()
{
$this->load->library('pagination');
$query=$this->db->count_all_results('users');
return $query;
}


And my pagination is showing like this: 1 2 >

Please see where is problem.

Answer

I've made some changes to your controller and model. Changes are commented:

Controller

// admin/provider/list_providers/3 <- Offset number
function list_providers($offset = null)
{    
    $admin_session_data= $this->session->userdata('user_logged_in');

    // Load models/libraries/helpers
    $this->load->model('admin/provider_model');
    $this->load->library('pagination');
    $this->load->helper('url');

    // Pagination config
    $config['base_url'] = site_url('index.php/admin/provider/list_providers') . '/';
    $config['total_rows'] = $this->provider_model->countprovider();
    $config['per_page'] = 3;
    // $config['creat_link'] = 1; <-- This is not a valid option
    $config['first_link'] = 'First';
    $config['last_link'] = 'Last';

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

    // If the pagination library doesn't recognize the current page add:
    $this->pagination->cur_page = $offset;

    $data['list']= $this->provider_model->listproviders($config['per_page'],$offset);

    if(!empty($data))
    {   
        $this->load->view('admin/admin_header',$admin_session_data); 
        $this->load->view('admin/admin_top_nav',$admin_session_data); 
        $this->load->view('admin/admin_left',$admin_session_data);
        $this->load->view('admin/provider_list',$data);
        $this->load->view('admin/admin_footer',$admin_session_data);
    }
}

Model

function listproviders($num,$offset)
{
    // $this->load->library('pagination'); <-- There is no need for this
    $query = $this->db->get('users', $num, $offset);

    // Check if pages where found
    if ($query->num_rows() > 0)
    {
        return $query->result_array();
    }
    else
    {
        return FALSE;
    }

}


function countprovider()
{
    // $this->load->library('pagination'); <-- There is no need for this
    return $this->db->count_all_results('users');
}