Let have a preview of result first.
That is an really simple pagination example with almost no interface design.
and the value in url is for controll which page to be displayed.
I would assume you got the basic knowledge about Codeniger.
That's mean I would skip the part about config the database.
If you don't know how to config the database,
please take a look of this link:
CodeIgniter User Guide Version 2.1.4 - Connecting to your Database
http://ellislab.com/codeigniter/user-guide/database/connecting.html
* * * *
Create
a database named “paging”
CREATE DATABASE paging;
Create
a table named “wp_posts”
CREATE TABLE wp_posts(id int(11),post_date timestamp,post_title text);
Insert
record into table named “wp_posts” for demo
INSERT INTO wp_posts VALUES('1', CURRENT_TIMESTAMP, 'Title 1'),('2', CURRENT_TIMESTAMP, 'Title 2'),('3', CURRENT_TIMESTAMP, 'This is Title 3'),('4', CURRENT_TIMESTAMP, 'This is Title 4'),('5', CURRENT_TIMESTAMP, 'Title 5 !'),('6', CURRENT_TIMESTAMP, 'Title 6~'),('7', CURRENT_TIMESTAMP, 'And this is Title 7~'),('8', CURRENT_TIMESTAMP, 'This is Title 8'),('9', CURRENT_TIMESTAMP, ' Title 9'),('10', CURRENT_TIMESTAMP, ' Title 10'),('11', CURRENT_TIMESTAMP, 'Title 11'),('12', CURRENT_TIMESTAMP, 'Title 12'),('13', CURRENT_TIMESTAMP, 'This is Title 13'),('14', CURRENT_TIMESTAMP, 'This is Title 14'),('15', CURRENT_TIMESTAMP, 'Title 15 !'),('16', CURRENT_TIMESTAMP, 'Title 16~'),('17', CURRENT_TIMESTAMP, 'And this is Title 17~'),('18', CURRENT_TIMESTAMP, 'This is Title 18'),('19', CURRENT_TIMESTAMP, ' Title 19'),('20', CURRENT_TIMESTAMP, ' Title 20'),('21', CURRENT_TIMESTAMP, 'Title 21'),('22', CURRENT_TIMESTAMP, 'Title 22'),('23', CURRENT_TIMESTAMP, 'This is Title 23'),('24', CURRENT_TIMESTAMP, 'This is Title 24'),('25', CURRENT_TIMESTAMP, 'Title 25 !'),('26', CURRENT_TIMESTAMP, 'Title 26~'),('27', CURRENT_TIMESTAMP, 'And this is Title 27~'),('28', CURRENT_TIMESTAMP, 'This is Title 28'),('29', CURRENT_TIMESTAMP, ' Title 29'),('30', CURRENT_TIMESTAMP, ' Title 30');
And then move to the Codeniger MVC coding:
Create a controller named "welcome.php"
Controller
<?php
class Welcome extends CI_Controller
{
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Countries");
$this->load->library("pagination");
$this->load->database();
}
public function example1() {
$this->load->library('pagination');
//$config['base_url'] is the base url of the pagination's link
$config['base_url'] = 'http://localhost/page/index.php?/welcome/example1';
//get amount of returning record from mySQL.
$config['total_rows'] = $this->Countries->record_count();
$config['per_page'] = 5; //display how many record per page.
//Get the begining display record number from the url.
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->Countries->fetch_countries($config["per_page"], $page);
$this->pagination->initialize($config);
$this->load->view("example1",$data);
}
}
?>
Create a model named "countries.php"
Model
<?php
class Countries extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
//Get results amount from table named "wp_posts".
return $this->db->count_all_results('wp_posts');
}
public function fetch_countries($limit, $start) {
/* Get results from table named "wp_posts",
with specified limit and start postition,
the values are passed from controllers. */
$this->db->limit($limit, $start);
$query = $this->db->get("wp_posts");
//Load result from database for return
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
?>
and the view named example1.php
View
<body>
<div id="container">
<h1>Posts</h1>
<div id="body">
<?php
foreach($results as $data) {
echo $data->post_date . " - " . $data->post_title . "<br>";
}
?>
<p><?php //echo $links; ?></p>
<p><?php echo $this->pagination->create_links(); ?></p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
</body>
Referance:
CodeIgniter User Guide Version 2.1.4 - Pagination Class
http://ellislab.com/codeigniter/user-guide/libraries/pagination.html
Pagination with CodeIgniter
http://www.sitepoint.com/pagination-with-codeigniter/
Latest code and version to download:
https://github.com/soyosolution/CodeIgniter-pagination-library
CodeIgniter User Guide Version 2.1.4 - Pagination Class
http://ellislab.com/codeigniter/user-guide/libraries/pagination.html
Pagination with CodeIgniter
http://www.sitepoint.com/pagination-with-codeigniter/
Latest code and version to download:
https://github.com/soyosolution/CodeIgniter-pagination-library
Is setting up database for pagination in CodeIgniter necessary or can we do it later, like done here: https://www.cloudways.com/blog/pagination-in-codeigniter/
ReplyDelete