CodeIgniter has many built-in classes and plugins. Pagination class of CodeIgniter is very easy and simple to use. Also Ajax Pagination library is availanle in Codeigniter. Here i will describe how to create simple ajax pagination in Codeigniter using some simple steps.
First of all download Ajax Pagination from Codeigniter site. And put this library in “system/libraries/” folder.
Along with library you need to download prototype.js file and put this file any where on root.
Here we have a table which we use for getting records and apply pagination on its records.
1 2 3 4 5 6 7 8 |
CREATE TABLE `my_best_friends` ( `f_id` int(11) NOT NULL auto_increment, `f_name` varchar(100) NOT NULL, `f_phone` int(11) NOT NULL, `f_email` int(11) NOT NULL, `f_address` int(11) NOT NULL, PRIMARY KEY (`f_id`) ) |
Your controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
class Paging extends Controller { function Paging(){ parent::Controller(); $this->load->helper(array('form','url')); $this->load->helper('text'); $this->load->library('Ajax_pagination'); } function index() { redirect ('paging/my_friends'); } function my_friends() { $pdata['TotalRec'] = $this->FriendsModel->TotalRec(); $pdata['perPage'] = $this->perPage(); $pdata['ajax_function'] = 'get_friends_ajax'; $subdata['paging'] = $this->parser->parse('paging', $pdata, TRUE); $subdata['all_friends'] = $this->FriendsModel->my_friends($this->perPage()); $data['body_content'] = $this->parser->parse('friend_list', $subdata, TRUE); $this->load->view('main',$data); } function get_friends_ajax() { $pdata['TotalRec'] = $this->FriendsModel->TotalRec(); $pdata['perPage'] = $this->perPage(); $pdata['ajax_function'] = 'get_friends_ajax'; $data['paging'] = $this->parser->parse('paging', $pdata, TRUE); $data['all_friends'] = $this->FriendsModel->my_friends($this->perPage()); $this->load->view('friend_list',$data); } function PerPage() { return 5; } } |
Model :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class FriendsModel extends Model { function FriendsModel() { parent::Model(); } function TotalRec() { $sql = "SELECT * FROM my_friends"; $q = $this->db->query($sql); return $q->num_rows(); } function my_friends($perPage) { $offset = $this->getOffset() $query ="SELECT * FROM my_friends Order By f_id Desc LIMIT ".$offset.", ".$perPage; $q = $this->db->query($query); return $q->result(); } function getOffset() { $page = $this->input->post('page'); if(!$page): $offset = 0; else: $offset = $page; endif; return $offset; } } |
View: paging.php in application/views/ folder
1 2 3 4 5 6 7 8 9 |
$config['first_link'] = 'First'; $config['div'] = 'container'; //Div tag id $config['base_url'] = base_url().'paging/'.$ajax_function; $config['total_rows'] = $TotalRec; $config['per_page'] = $PerPage; $config['postVar'] = 'page'; $this->ajax_pagination->initialize($config); echo $this->ajax_pagination->create_links(); |
View: friend_list.php in application/views/ folder
1 2 3 4 |
foreach($all_friends as $row) { } |
Friend Name | Friend Phone | Friend Address |
echo $row->f_name | echo $row->f_phone | echo $row->f_address |
1 |
Main home file: main.php in application/views/ folder
This file contains your html code and all css and js files.
I have just given main container where your data will show. you have to add html and css/js files your self.
1 |
give me a running code zip file
use this one…
http://www.joelsays.com/simple-ajax-pagination-in-codeigniter/
http://www.joelsays.com/simple-ajax-pagination-in-codeigniter/
overwrite thw .htaccess file
Where is ajax