In these last two weeks, I’ve received bunches of requests for a CodeIgniter pagination screencast; so that’s what we’ll be reviewing today! As a bonus, we’ll also take a look at the super convenient HTML Table class.
Catch Up
- Day 1: Getting Started With the Framework
- Day 2: Database Selecting Methods
- Day 3: Sending Emails
- Day 4: Newsletter Signup
- Day 5: CRUD
- Day 6: Login
Day 7: Pagination
Other Viewing Options
Thank you, Screencast.com!
- Follow us on Twitter, or subscribe to the Nettuts+ RSS Feed for more daily web development tuts and articles.



Great tut Jeff, been watching them all.
However, I don’t know if it is the newer version of CI (seeing as this is 2 years old) or just my code, but your version did not work for me.
Whenever I clicked on the anchor to go the next page it gives me a 404 error.
This can be fixed by:
- Adding $config['uri_segment'] = X to the config data with x being the segment, then using that info in getting the data from the db
Here is (some of) my code if you are experiencing the problem above.
//Controller
$config['per_page'] = 10;
$config['num_links'] = 2;
$config['uri_segment'] = 4;
if ($query = $this->admin_model->get_records($config['per_page'], $config['uri_segment'])) {
$data['records'] = $query;
}
//Model
function get_records($per_page, $uri) {
$query = $this->db->get(‘data’, $per_page, $this->uri->segment($uri));
return $query->result();
}
And this then works :).
One of the best tutorials that i have found regarding pagination in codeigniter
http://blog.avinash.com.np/2010/07/07/easy-pagination-and-sorting-codeigniter/
The “Download the Video” link is broken
Can you please fix the download video link? Also I’ve had no success in starting the video here on nettuts.
The “FLV version” link for blip.tv does work though.
Hi jeff,
I am learning codeigniter and in fact learned mostly from ur tutorials. I want to know after learning these things, are they sufficient for building a website or we should use codeigniter cms like pyrocms? or should we use codeigniter with some css and html to build websites? I think there is very little or no coding required in pyrocms then what’s the purpose of learning codeigniter?
Download Link isnt w0rking :(
Video is not working
Please reupload video. I’m badly in need for pagination tutorial / ci … thanks much jeffrey..
One question, can I actually put this pagination configuration inside the configs folder and name the file name pagination.php like what we did in email?
I have a site where I’m using pagination on, just like you showed me in the tutorial Jeffrey, and the pagination of the database results works but the pagination link won’t show I am using the four URI segment instead of the third for the offset. I even did
$config['uri_segment'] = 4and in the view I didpagination->create_links(); ?>. This is how i am giving the db results to my view. What am i doing wrong?$someData = $this->db->order_by('time_uploaded','desc')->where(array('type' => 'photo'))->get('media',$config['per_page'], $this->uri->segment('4'));
$data['photo_data'] = $someData->result_array();
$this->load->view('templates/main', $data);
thanks…
Has anyone else had trouble getting the table class to generate a table of data that has been returned from your model?
Like This:
Model:
db->get($table,$limit,$offset);
return $q->result();
}
Controler:
load->library(‘table’);
$this->load->model(‘category_model’);
$pag_config['base_url'] = ‘http://localhost:8888/klassy/index.php/MyControler/MyFunction’;
$pag_config['total_rows'] = $this->db->get(‘My_Table’)->num_rows();
$pag_config['per_page'] = 2;
$pag_config['num_links'] = 5;
$pag_config['full_tag_open'] = ”;
$pag_config['full_tag_close'] = ”;
$this->pagination->initialize($pag_config);
$data['query'] = $this->category_model->getRecords(‘My_Table’,$pag_config['per_page'],$this->uri->segment(3));
$data['main_content'] = ‘categories_view’;
$this->load->view(‘includes/template’,$data);
//$this->load->view(‘categories_view’);
}
View:
pagination->create_links();
echo $this->table->generate($query);
echo $this->pagination->create_links();
I’m totally stumped and the Codeignitor forums have been no help.
For putting in the values quick if anyone wants to know, I created this horrible excuse for a script and dropped it in my root and ran it. “data” was already in my db ci_series from the previous tuts.
Oh, and don’t forget to delete things like this after using them if you do things like this on your server. Wouldn’t want some jerk thinking it was funny to run it over and over or get your root pass from it.
$con = mysql_connect(‘localhost’, ‘root’, ‘yourpassword’);
if($con){
if($dbSelect = mysql_select_db(‘ci_series’)){
$sql = “INSERT INTO `ci_series`.`data` (`id`, `title`, `contents`) VALUES (NULL, ‘Random Title’, ‘Some random text that nobody cares about’);”;
for($i = 0; $i <= 100; $i++)
{
$result = mysql_query($sql)or die('problem inserting:' . mysql_error());
}
echo "done";
mysql_close($con);
}
}
Funny thing, I did the first insert in phpmyadmin and then told it to generate the php code and the php code it generated didn’t work. I had to go back and remove all the “\”‘s it put in.
I was not Understanding The Pagination
:) but Finally I found The Error
it’s great tool in codeigniter Framework
I think I will Change All my sites to be developed by Codeigniter
Thanks for your help
Referencing some Javascript on another server just to implement table striping seems a bit odd especially when there’s a better way (and I understand you broke a few rules here) right at your fingertips. Just put this in the template before you generate the table:
<?php
$tmpl['row_alt_start'] = ‘<tr class=”striped”>’;
$this->table->set_template($tmpl);
?>
Then all you have to do is apply css background color to the “tr.striped” selector. Simple.
Somebody else may have caught this but I didn’t take the time to wade through four pages of comments. And perhaps this ability wasn’t available in the CodeIgniter version you are working in, or you don’t use it because it may change or deprecate in future versions.
In any case, great tutorials! After these, I’ll be more than comfortable enough to gulp the user manual.
Great tutorial , thank youuu :)
Another great tutorial Jeffrey. Can’t believe how easy codeIgniter makes pagination. That table generate method is just amazing too :D
Thanks Jeff, you’re awesome!
Love the tutorials, very helpful