When you perform a search on Google, the results are displayed in a list and you have the option to click through pages of the results. That is an example of pagination. Pagination simply means the user has the ability to click through the pages of results, viewing a subset of them each time, rather than having to scroll down the screen for ages.
Pagination is particularly useful when you are coding an application that interfaces with a database. A large dataset might have hundreds of possible results for one query, and pagination creates a much nicer user experience.
In this tutorial, I’ll use CodeIgniter’s pagination library to show you how you can create a paginated list of results from a MySQL database. Along the way, you’ll also see how to fix a problem with the pagination links that the library might produce.
The Model
We’ll start by creating a model in the application which needs to do two things: provide a count of all the records in the
Country
table, and retrieve a list of countries from the table. Save the following as models/countries.php
:class Countries extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("Country");
}
public function fetch_countries($limit, $offset) {
$this->db->limit($limit, $offset);
$query = $this->db->get("Country");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
or
public function fetch_countries($limit, $offset) {
$this->db->limit($limit, $offset);
$query= $this->db->query("select * from Country limit {$limit} OFFSET {$offset}");
return $query->result();
}
}
CodeIgniter’s database class with Active Record is used to count and retrieve the data from the database.
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 library will use this along with other options we set to work out how to divide the results up into pages.
The
fetch_countries()
method retrieves a list of all the records from the Country
table. There are two arguments for this method: $limit
and $start
. They will be passed into the query to determine how many records to return, and what record to start from. The arguments will be set in the controller.The Controller
Next, we need to create a method in the default
Welcome
controller (controllers/welcome.php
) called example1()
. But just before we do that, we’ll need to load the model and also the pagination library in the class’ constructor.<?php
class Welcome extends CI_Controller
{
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Countries");
$this->load->library("pagination");
}
public function example1() {
$config = array();
$config["base_url"] = base_url() . "index.php/welcome/example1";
$config["total_rows"] = $this->Countries->record_count();
$config["per_page"] = 20;
$config['num_links'] = 5;
$config['use_page_numbers'] = TRUE;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['first_link'] = '« First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last »';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3))? $this->uri->segment(3) : 1;
if($page==1)
{
$offset=0;
}
else
{
$offset=$config['per_page'] * ($page-1);
}
$data["results"] = $this->Countries->
fetch_countries($config["per_page"], $offset);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$this->load->view("example1", $data);
}
}
The
example1()
method starts by providing some of the most common configuration options for the pagination library. The options are placed in an array which is then passed as an argument to the library’s initialize method. The library requires the options must pass a base URL, a total row count, how many rows per page we want, and which part of the URL will contain the page section the user is using.
Remember those parameters that were set required by the query for a list of countries (
$limit
and $start
)? Those are set next in the call to the model’s fetch_countries()
method. The $page
variable uses the ternary operator to either set its value to whatever is in the third segment of the URI string or to zero (meaning the user is on the first page).
Finally, the
create_links()
method of the pagination library creates the pagination links and then we load the view to display the outcome.The View
For the view file, we can copy the
views/welcome_message.php
and called it example1.php
and replace most of the content of the body with the following:<body>
<div id="container">
<h1>Countries</h1>
<div id="body">
<?php
foreach($results as $data) {
echo $data->Name . " - " . $data->Continent . "<br>";
}
?>
<p><?php echo $links; ?></p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
</body>
0 comments:
Post a Comment