Tuesday, October 29, 2013

How To Implement Pagination In Codeigniter ion_auth library?

This is how I implemented pagination customized using bootstrap styles in Codeigniter ion_auth Library.
Controller
	function display_user()
	{
		$this->data['title'] = "Display User";

		if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
		{
			redirect('auth', 'refresh');
		}

		$this->load->library('pagination');
		$config = array();
		$config["base_url"] = base_url() . "auth/display_user/";;
		$config['total_rows'] = $this->ion_auth->users()->num_rows();
		$config['per_page'] = '30';

		//pagination customization using bootstrap styles
		$config['full_tag_open'] = ' <ul class="pagination pull-right">';
		$config['full_tag_close'] = '</ul><!--pagination-->';
		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page">';
		$config['first_tag_close'] = '</li>';

		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page">';
		$config['last_tag_close'] = '</li>';

		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page">';
		$config['next_tag_close'] = '</li>';

		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page">';
		$config['prev_tag_close'] = '</li>';

		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';

		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';


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

		$the_uri_segment = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$config['uri_segment'] = $the_uri_segment;


		//list the users
		$this->data['users'] = $this->ion_auth->offset($this->uri->segment($the_uri_segment))->limit($config['per_page'])->users()->result();

		foreach ($this->data['users'] as $k => $user)
		{
			$this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
		}

		$this->data['links'] = $this->pagination->create_links();
		//set the flash data error message if there is one
		$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');


		$this->data['include'] = 'auth/display_user';
		$this->_render_page('auth/template', $this->data);

	}
Views
 <?php echo $links;?>
//output the proper pagination links styled with twitter bootstrap css

No comments:

Post a Comment