How To create Pagination in codeigniter
Here I am Create Pagination Example in Codeigniter.
The Model
The record_count( ) method returns the number of records and is needed because one of the options in the $config array for the pagination library is $config[ "total_rows"]
The get_news( ) method retrieves a list of all the records from the News table.
<? php class News_model extends CI_model{
$this->load->database( );
}
}
$this->db ->limit( $limit, $start);
$query = $this->db ->get( "news");
$data[ ] = $row;
}
/*$query = $this->db ->get( 'news');
}
}
}
The Controller
Here We can Load model and pagination library and also added helper of
<? php class News extends CI_Controller {
$this->load->model( 'news_model');
$this->load->helper( "url");
$this->load->library( "pagination");
}
$this->load->helper( "url");
$config = array( );
$config[ 'base_url'] = base_url( ).'index.php/news/';
$config[ 'total_rows'] = $this->news_model->record_count( );
$config[ 'per_page'] = 2;
$config[ "uri_segment"] = 2;
$this->pagination->initialize( $config);
$page = ($this->uri ->segment( 2)) ? $this->uri ->segment( 2) : 0;
$data[ "results"] = $this->news_model->get_news( $config[ "per_page"], $page);
$data[ "links"] = $this->pagination->create_links( );
//$data[ 'news'] = $this->news_model->get_news( );
$data[ 'title'] = 'News archive';
$this->load->view( 'templates/header', $data);
$this->load->view( 'news/index', $data);
$this->load->view( 'templates/footer');
}
}?>
The View
<? php
//echo "<pre>";
//print_r( $results);
<h2><? php echo $result->title; ?></h2>
<div class="main">
<? php echo $result->text; ?>
<p><a href="view/<? php echo $result->slug; ?>">View article</a></p>
</div >
<? php }
$route[ 'news/(: num )'] = 'news/index';
0 komentar:
Post a Comment