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.


Add Comment

Discussion 237 Comments

Comment Page 4 of 4 1 2 3 4
  1. Ben Stokoe says:

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

  2. Avk says:

    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/

  3. EmpreJorge says:

    The “Download the Video” link is broken

  4. Husar says:

    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.

  5. rk says:

    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?

  6. Fariya says:

    Download Link isnt w0rking :(

  7. V says:

    Video is not working

  8. SVINE says:

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

  9. Jubic says:

    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?

  10. Ethan Kramer says:

    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);

  11. prachee says:

    thanks…

  12. halledega says:

    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.

  13. Ian says:

    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.

  14. 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

  15. Stoutie says:

    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.

  16. labidi says:

    Great tutorial , thank youuu :)

  17. Robo says:

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

Comment Page 4 of 4 1 2 3 4

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.