CodeIgniter from Scratch Day 7: Pagination
videos

CodeIgniter from Scratch Day 7: Pagination

This entry is part 7 of 17 in the CodeIgniter From Scratch Session
« PreviousNext »

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 7: Pagination

Other Viewing Options

Pagination

Thank you, Screencast.com!


Screencast.com

…for providing the hosting for these video tutorials.


Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://benstokoe.co.uk Ben Stokoe

    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 :).

    • aumatin

      ..still it didn’t work for me:-(

  • Avk

    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/

  • EmpreJorge

    The “Download the Video” link is broken

  • Husar

    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.

  • rk

    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?

  • Fariya

    Download Link isnt w0rking :(

  • V

    Video is not working

  • SVINE

    Please reupload video. I’m badly in need for pagination tutorial / ci … thanks much jeffrey..

  • Jubic

    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?

  • Ethan Kramer

    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'] = 4 and in the view I did pagination->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);

  • prachee

    thanks…

  • halledega

    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.

  • Ian

    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.

  • http://www.alldirs.com/ Mohamed Alsemany

    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

  • http://blog.jstoutenburg.com Stoutie

    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.

  • labidi

    Great tutorial , thank youuu :)

  • Robo

    Another great tutorial Jeffrey. Can’t believe how easy codeIgniter makes pagination. That table generate method is just amazing too :D

  • Andrea

    Thanks Jeff, you’re awesome!

    Love the tutorials, very helpful

  • http://www.iamyabee.co.cc yabee

    what if I cant use tables? how do I display the data?

  • http://geeksarsenal.comli.com Jahanzeb Khan

    When i click a page for example page 2, i get a 404 error, say i clicked 2 to go to page 2

    http://127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/5

    as you can see the url is repeated twice :/

    and if i try remove the base url thing, then it links to

    http://127.0.0.1:8887/

    Here are my settings

    $config['base_url'] = ’127.0.0.1:8887/codeigniter-tests/index.php/pagination/index/’;
    $config['total_rows'] = $this->db->get(‘data’)->num_rows;
    $config['per_page'] = 5;
    $config['num_links'] = 10;
    $config['uri_segment'] = 3;
    ?>
    :/

    • MKK

      your $config['base_url'] should look like this
      $config['base_url'] = ’127.0.0.1:8887/codeigniter-tests/’;

      MKK.

  • Nicky

    Thanks Sir,

    Such a nice tut this is. I am in learning stage of CI and this contents helps me so much to learn CI.

  • tony

    It would have been nice to include the SQL file in the Source Code so people didn’t have to spend time creating 100 rows in the DB to get this to work.

    • John K

      in phpmyadmin, it’s pretty simple. just click on SQL tab, type in

      insert data set title=’How to be Great’, content=’Content about how to be great goes here.’

      then keep clicking Go

  • Shiva

    excellent, pagination will be up in no time with CI

  • Khattak

    Sir if we put controller and view files in Admin directory then pagination not working. i have changed the base url but its not working

    any one help me

  • Peter Michealson

    how about information in URL link. I dont want to see like index/60 where 60 is the offset but I’d like to see smth like: /page/1 … page/2 etc. thanks

  • pantho

    Hello Jeff it’s a really helpful video………But I want to show my pagination like this———————
    ” First-1-2-3-…..-Last ” just like your own pagination system. I am newbie in codeigniter, & it’s my 3rd week.So please anybody help me.