Tutorial on Making Pagination in Codeigniter

Dear reader. There was a question emailed to me about how to make pagination in Codeigniter. Actually it’s easy alias easy, because Codeigniter already provides a library to do / create pages on a view data that we take from the database. Here’s how to make simple pagination my style (my own way 🙂 ).

Okay, create a database on your MySQL system, let’s say I created it with the name db_gaji.

CREATE DATABASE db_gaji;

Then create a table of employees which we will later use as the data source for our tutorial.

CREATE TABLE IF NOT EXISTS tbl_employee (
nik int(15) NOT NULL AUTO_INCREMENT,
full_name varchar(100) NOT NULL,
jenkel enum(‘L’,’P’) DEFAULT NULL,
birth_date date NOT NULL,
gapok int(11) NOT NULL,
PRIMARY KEY(nik)
)

and fill in the employee table with the following data

After creating the table, the next step is to set the connection to our database, how to open the application/config/database.php file, on line 51 you will find the following script:

$db[‘default’][‘hostname’] = ‘localhost’;
$db[‘default’][‘username’] = ‘root’;
$db[‘default’][‘password’] = ‘admin’;
$db[‘default’][‘database’] = ‘db_salary’;
$db[‘default’][‘dbdriver’] = ‘mysql’;
Also change the file in the application/config/autoload.php directory like this:

[php]$autoload[‘libraries’] = array(‘database, pagination’);[/php]

useful for setting auto database library.

And also do a load helper to access the uri segment class to function for the pagination.

[php]$autoload[‘helper’] = array(‘url’);[/php]

Modeling

After everything is ready, it’s time for us to create a model, I named it m_karyawan.php. What is its function? its function is to retrieve from the database the records that we need to display on the browser screen via files in the view directory. Make it like this: