Codeigniter Pagination
![]() |
Codeigniter Pagination |
Follow the below example to learn how to create codeigniter pagination with database..
=>Database and Table:
Database Name: test
Table Name : company
-- Table structure for table `company`
1 2 3 4 5 | CREATE TABLE IF NOT EXISTS `company` ( `id` int (11) NOT NULL AUTO_INCREMENT, ` name ` varchar (100) NOT NULL , PRIMARY KEY (id) ) ENGINE=MyISAM AUTO_INCREMENT=60 DEFAULT CHARSET=latin1; |
-- Dumping data for table `company`
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 | INSERT INTO `company` (`id`, ` name `) VALUES ( NULL , 'Nokia Inc' ), ( NULL , 'Samsung Inc' ), ( NULL , 'Sony Inc' ), ( NULL , 'DigiCraft' ), ( NULL , 'Techsoft' ), ( NULL , 'Newsoft' ), ( NULL , 'MonoSoft' ), ( NULL , 'Digisoft' ), ( NULL , 'Noki Android' ), ( NULL , 'Samsung Shipyard' ), ( NULL , 'Bajaj Motor' ), ( NULL , 'Tata Nano' ), ( NULL , 'HAL Inc' ), ( NULL , 'Airtel Inc' ), ( NULL , 'Baidu Inc' ), ( NULL , 'Holdings Inc' ), ( NULL , 'Nokia Inc' ), ( NULL , 'Samsung Inc' ), ( NULL , 'ASUS Inc' ), ( NULL , 'IBM' ), ( NULL , 'Alibaba Inc' ), ( NULL , 'Google Inc' ), ( NULL , 'FileMaker Inc' ), ( NULL , 'Facebook Inc' ), ( NULL , 'EMC Corporation' ), ( NULL , 'Ebay Inc' ), ( NULL , 'Delphi Inc' ), ( NULL , 'Dell Inc' ), ( NULL , 'Bank of India' ), ( NULL , 'Bank Of America' ), ( NULL , 'Bally Technology Inc' ), ( NULL , 'Bharathi Airtel' ), ( NULL , 'Alphabet Inc' ); |
Configuration:
autoload.php
application/config/autoload.php
1 | $autoload [ 'helper' ] = array ( 'url' ); |
config.php
application/config/config.php
1 |
Database.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | $active_group = 'default' ; $query_builder = TRUE; $db [ 'default' ] = array ( 'dsn' => '' , 'hostname' => 'localhost' , 'username' => 'root' , 'password' => '' , 'database' => 'test' , 'dbdriver' => 'mysqli' , 'dbprefix' => '' , 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production' ), 'cache_on' => FALSE, 'cachedir' => '' , 'char_set' => 'utf8' , 'dbcollat' => 'utf8_general_ci' , 'swap_pre' => '' , 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array (), 'save_queries' => TRUE ); |
Codes :
Controllers: blog.php
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 | <?php class Blog extends CI_Controller{ public function index(){ //Loading Pagination Library and Model $this ->load->library( 'pagination' ); $this ->load->Model( 'Pagination_model' ); //Count total rows $rows = $this ->Pagination_model->record_count(); $config [ 'base_url' ]=base_url(). 'index.php/blog/index' ; $config [ 'total_rows' ]= $rows ; $config [ 'per_page' ]=2; $config [ 'prev_link' ]= 'Previous' ; $config [ 'next_link' ]= 'Next' ; $config [ 'display_pages' ] =TRUE; $config [ 'full_tag_open' ] = '<div class="pagination">' ; $config [ 'full_tag_close' ] = '</div>' ; $config [ 'cur_tag_open' ] = '<a class="active" href="">' ; $config [ 'cur_tag_close' ] = '</a>' ; $this ->pagination->initialize( $config ); $perpage =2; if ( $this ->uri->segment(3)) { $segment = ( $this ->uri->segment(3)); } else { $segment =0; } $data [ 'user' ] = $this ->Pagination_model->user( $perpage , $segment ); $this ->load->view( 'index' , $data ); } } |
Models : pagination_model.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php class Pagination_model extends CI_Model{ public function __construct(){ parent::__construct(); $this ->load->database(); } //Count all records of table user_error public function record_count(){ return $this ->db->count_all( 'company' ); } public function user( $perpage , $segment ){ $query = $this ->db->limit( $perpage , $segment )->order_by( 'id' , 'desc' )->get( 'company' ); return $query ->result_array(); } } ?> |
Views: index.php
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 49 50 51 52 53 | < style > .pagination { display: inline-block; } .pagination a { color: black; float: left; padding: 4px 8px; text-decoration: none; border: 1px solid #ddd; } .pagination a.active { background-color: #4CAF50; color: white; border: 1px solid #4CAF50; } .pagination a:hover:not(.active) {background-color: #ddd;} .pagination a:first-child { border-top-left-radius: 5px; border-bottom-left-radius: 5px; } .pagination a:last-child { border-top-right-radius: 5px; border-bottom-right-radius: 5px; } </ style > < h3 style = "color:red" >Codeigniter Pagination</ h3 > < table border = "1" cellspacing = "0" width = "400" > < tr > < th >Serial</ th > < th >ID</ th > < th >Name</ th > </ tr > <? php $i = $this->uri->segment(3); foreach ($user as $row){ $i++; ?> < tr > < td ><? php echo $i;?></ td > < td ><? php echo $row['id'];?></ td > < td >< b ><? php echo $row['name'];?></ b ></ td > </ tr > <? php } ?> </ table > <? php echo $this->pagination->create_links(); ?> |
That's it guys how to create codeigniter pagination with database..Keep visiting for more codes...
Thank You..
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.