Using codeigniter
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.
When you perform a search on Google ,Search results will appear in a list,and you can choose to click on the search results page. This is an example of pagination. Pagination simply means that the user can click through the results page & # xff0c; to view a subset of the results at a time & # xff0c; without having to scroll down the screen for a while.
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.
Pagination is especially useful when you are coding an application that interfaces with a database. Large data sets may have hundreds of possible results for a query, while pagination provides a better 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.
In this tutorial,I’ll use CodeIgniter’s pagination library to show you how to create a paginated list of results from a MySQL database. Along the way,you’ll also see how to troubleshoot pagination linking issues your library might generate.
I’ll assume you have an installation of CodeIgniter 2.1 ready to go. For the database, I’ll use the official sample database provided by MySQL that available for download as an SQL file.
I assume you are ready to install CodeIgniter 2.1. For the database , I will be using the official sample database provided by MySQL , which is available for download as a SQL file.
The Model (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
: p>
We will first create a model in the application ,This model needs to do two things :provide a count of all records in the Country
table,and Retrieves a list of countries from a 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, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get ("Country");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
CodeIgniter’s database class with Active Record is used to count and retrieve the data from the database.
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.
record_count()
method returns the number of records , this is needed, because one of the options in the $config
array for the pagination library is $config["total_rows" ]
. This library will be used in conjunction with other options we set,to determine how to split the results 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 fetch_countries()
method retrieves a list of all records from the Country
table. This method has two parameters : $limit
and $start
. These will be passed into the query,to determine how many records to return and which one to start with. The parameters will be set in the controller.
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 paginationre-calculate the digit links as each page is accessed. The first thing we can do is to disable the first/last previous/next links to tidy up the navigation. It doesn’t stop the expanding digits, but it does tidy things up .
There is a configuration item that may help with this. It may be useful at this stage to remember that & # xff0c; we are retrieving records from a database & # xff0c; and that CodeIgniter has to recalculate numeric links when each page is visited & # xff0c; this may be useful. The first thing we do is disable the first/last previous/next links to tidy up the navigation. It won’t prevent the expansion of numbers,but will tidy it up.
The second thing we can do is calculate how many pages there are by dividing the total rows by the number of rows required per page, round the result, and pass it to the $config[" num_links"]
parameter.
The second thing we can do is calculate how many pages there are by dividing the total rows by the number of rows required per page ,round the result& #xff0c; then pass it to the $config["num_links"]
parameter.
Here’s what the example1()
method looks like with those changes:
After these changes , example1()
The method looks like this :
public function example1() {
$config["base_url"] = base_url () . "welcome/example1";
$config["total_rows"] = $this->Countries->record_count();
$config["per_page"] = 20;
$config["uri_segment"] = 3;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links" ] = round($choice);
$this->pagination->initialize($config);
$page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;
$data["results"] = $this->Countries
->fetch_countries($config["per_page"], $page );
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
}
This will improve the links now.
This will improve the links now.
Summary(Summary)
Now you know how to use the most useful configuration options for the CodeIgniter pagination library, and you can also fix the way the pagination links are displayed to provide a consistent experience for your users. A user guide comes with every download of CodeIgniter, so be sure to check the other configuration options for the library there. It contains options for styling the pagination links, and changing the way the links are rendered on the page.
Now , you know how to use the most useful configuration options for the CodeIgniter Pagination Library , and also fix how pagination links are displayed, to provide a consistent experience for your users. Every download of CodeIgniter comes with a user guide , so make sure to check out the library’s other configuration options. It contains options for styling pagination links and changing how links are rendered on the page.
Image via Alexis Puentes / Shutterstock
Image via Alexis Puentes / Shutterstock
And if you enjoyed reading this post, you’ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like Jump Start PHP.
And ,If you enjoyed reading this article,You will love Learnable’s ; The place to learn fresh skills from the masters. Members get instant access to all of SitePoint’s eBooks and interactive online courses , such as Jump Start PHP.
Comments on this article are closed. Have a question about PHP frameworks? Why not ask it on our forums?
Comments on this article are closed. Have questions about PHP frameworks & #xff1f; Why not ask on our forums
Translated from: https://www.sitepoint.com/pagination-with-codeigniter/
use codeigniter