Try Tuts+ Premium, Get Cash Back!
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
  • Steven

    Great tutorial jeff, how would one use this framework to show the records as blog posts rather than in a table?

  • William Rouse

    Jeff:
    Please come back with more CI stuff. I am late to the tutorial and this was lovely. Please keep your promise in the first tutorial where you would develop a full web site.
    Thanks for doing this series.

  • William Rouse

    One more thought.
    The one way I could get the pagination program to work is to change the line on my machine from:
    $config['base_url'] = ‘http://localhost/ci_day7/index.php/site/index;
    to
    $config['base_url'] = base_url().’?site/index/’;
    which is actually thhe same except for the question mark. I have been in the user guide for over an hour and I can’t figure this out. Where can I read about the question mark and that syntax.

    • http://msylvia.nukefile.net MSylvia

      Another is modifying the .htaccess file and adding the “site/index/” to it.

      RewriteRule ^(.*)$ /ci/index.php?/site/index/$1 [L]

      This fixed the problem for me when using mod_rewrite to remove the index.php.
      My base url: http://localhost/ci/

      • http://msylvia.nukefile.net MSylvia

        Sadly that would also break everything else. So almost there.

    • http://msylvia.nukefile.net MSylvia

      Config:
      $config['base_url'] = base_url().’site/index/’;

      With a rewrite rule:
      RewriteRule ^(.*)$ /ci/index.php?/$1 [L]

      Generates links like this:
      http://localhost/ci/app/index/1

      Fixed!

      PS Great Tutorial. Hope to see more on CI soon. I love it.

  • Walter

    Hi Jeff:
    I find your screencasts really usefull, I mean… FANTASTIC.
    I’d suggest ci+ajax+jquery for next one.

    Come on J.! lot of people will be more than happy to watch more from you.
    Walter

  • Walter

    HI jeff,
    The link for this screencast is not working. Same is happening for day-6 too.
    Can you restore it?
    Thanks
    Walter

  • Rich

    Two months since last CI tutorial, guess it’s officially DOA. :( I’ve enjoyed these and they have helped me take my coding to the next level. Thanks Jeff.

  • http://www.fringl.com Grimm

    Thanks for the excellent tutorials!
    To my best regret links for last two – 6 and 7 are broken.
    Anyway, after long search in www, I found out Day 7 –

    http://blip.tv/file/2670407

    Unfortunately, I couldn’t find Day 6.

    Jeff, please – Christmas is coming, make us a present and restore Day 6 and 7 :)

    Best Regards

  • Ben

    Come on man – don’t stop them now – you said you’d do 10 days…

    If these have stop you could at least say so?

    Thanks have really enjoyed them.!

  • http://www.adiadi.info Adi

    Thank you Jeff,
    I just created a site for a client using codeigniter which i learned from your tutorials.
    You’ve helped me a lot.

    Thanks again.

  • Mark

    Thanks Jeff for these, hope you will continue with it !

    For those looking for Day 6 and Day 7 you can download them from Rapidshare.

    http://rapidshare.com/files/318456608/nettuts_codeigniter_day6.zip

    http://rapidshare.com/files/318603335/nettuts_codeigniter_day7.zip

  • Mark

    For those looking for Day 6 and Day 7 you can download them from Rapidshare.

    http://rapidshare.com/files/318456608/nettuts_codeigniter_day6.zip

    http://rapidshare.com/files/318603335/nettuts_codeigniter_day7.zip

    Jeff thanks for these great tutorials, hope you will continue with them !

  • http://www.shopsheep.com kristof
  • Walter

    rapidshare links are just from premium users….

  • Mini0n

    The next chapter of this series would be a great Christmas gift… ^_^

  • Jerin

    I have not got a codeigniter pagination that shows the uri segment as 1 for the first page,2 for second page and so on…. Instead using Codeignitr pagination the url is offset by 1. ie: its showing 1 for the secnd page, 2 for the 3rd page and so on….

    Can u please give me a code so that i can do this.

  • Fábio Antunes

    I’ve enjoyed your tutes, specially about Code Igniter, they’re really great.

    I haven’t had the chance to go around all of them, but once you encourage comments and tute proposals for this series. I have one :=D

    Could you do one, about the benefits about the URL system/class in place with Code Igniter, but most of all how to use them wisely.

    It would be really great.

  • http://jasonschleifer.com jason

    Just wanted to add in how awesome these tutorials are.. You totally make CI and MVC a joy to work with!

    thanks!

  • http://www.woony.be woony

    is there a day 8 , 9 and 10 coming???

  • http://msylvia.nukefile.net MSylvia

    Wonderfull tutorials! I agree more on this subject would be great.

  • http://www.radiart.pl Radi

    Thx Very Good Tutorial! Keep going! =P

  • Tom Van Assche

    Please continu this great series!

  • eric

    Please more CI!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • David Fitzgibbon

    Yeah more Codeigniter!

    Even just a quick tip on how to get rid of the index.php, havent gotten a hacj to work yet!

    • Dave

      1) Create .htaccess in the root of your app – where the main index.php is

      2) paste this into it:
      DirectoryIndex index.php
      RewriteEngine on
      RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

      3) Ensure mod_rewrite is installed/running on Apache

      4) Ensure Allowoverride is set to ALL (“Allowoverride All”) in Apache settings (should be “httpd.conf” in Apache, “000-default” in the ‘sites-enabled’ folder in Apache 2)

      SHOULD work. I had a bit of trouble because I had forgotten that I had a second set of apache config files for SSL, but after I changed Allowoverride in that, it was golden…

      • Joey

        Thank you!

        Finally got to the point where my uri->segments were blank, only solution was a bad ModRewrite. Thanks for your ModRewrite, works straight away!

  • Vladan

    Is it possible to see a chapter 8 in near future? Your tutorials are very helpful for learning CodeIgniter.

  • emcgfx

    Maybe you could do some more database tutorials for CodeIgniter from Scratch Day 8 I would like to see how to store categories in database, show then on page with pagination. Then results under those categories with pagination support also. Thanks.

  • alex

    i am in need of another Codeigniter day :)

  • Alexander

    Hay Jeffrey I was wondering if you would consider doing a tutorial on dynamically loading pages, like in a blog where you have one source file that gets a page id and loads the data. I know how to do it in php but i do not know how to approach it using MVC.

    –thanks!

  • eric

    I swear you miss one F’ing character and you can piss away hours of time……

    I am learning though. The hard way………

    • Alexander

      ya i know exactly what you mean! it can get pretty tedious sometimes.

      • Kars

        Try using a PHP editor/IDE with code debugger. It searches the errors for you.

  • http://teacher-x.com MJB

    I moved to new host and messed up CI layout format. Didnt change anything, can someone email me what happen is it server permissions? See at http://www.teacher-x.com

    THANKS

  • Kasper

    Really nice tuts and learned a lot from them, but I was thinking if there isent another way to do this, because this way I don’t really know how to tamper with the actual results, set I only want to show 100 chars of the content part? I know there is a way to change the layout of the table to divs, but it would be really nice with a chance to tamper the actual data.

  • http://filmsamlare.se Thim

    Awesome tut as always Jeffrey!

    Realley helped me :)

  • saad

    but if we won’t display the id , how can we do this ?

  • Miki

    Greetings from Serbia! Thank you for your lessons that contribute to the development of better web applications!

  • http://www.mesmerlab.com Jason

    I’m having an issue with the pagination and the routes.php file. Can you retain the clean URLs? I have to put in the full url in the base_url config parameter, so the subsequent pages are much longer than the others.

    And I had to use rsegment rather than the simple segment. It works now, but with the ugly URLs.

    Could there be another AJAX pagination solution where the URL itself doesn’t change?

  • http://gravityfx.org/ emcgfx
  • http://www-scf.usc.edu/~nvenkatr/ Nikhil

    Hi i love the CI series ..plzzz keep them coming..I had a question ..im working on a similar project but i need the table data to be links..for eg with this case i need each of the title and content to be links …is that possible ??please help!!

  • Dave

    @Jeffrey Way: “what did that take us – just a few minutes?” – a little longer, I’m using a MSSQL database :P
    Up to this point, the series has been perfectly compatible with MSSQL, this one is a little more involved due to MSSQL’s lack of the OFFSET feature (so essentially, active records just spits out the query “select top 10 * from data” as it’s MSSQL driver has no idea what to do with the OFFSET clause we specified in get(). Instead, a little ingenuity has to be forged using MSSQL’s BETWEEN clause.

    For anyone interested, here’s the resulting code for the Site class using MSSQL + pagination…
    http://codeigniter.com/forums/viewthread/55934/#734541

    :)

  • http://www.pcbsd.pl piker

    How does it pagination works with custom query ?
    $this->db->query(‘ SELECT * FROM table JOIN table2 WHERE table.id = table2.id_table2 ‘);

    I don’t want to run two queries to make a pagination.

    • harman

      in controller:

      $config['base_url'] = base_url().”admin/users/index/”;
      $config['total_rows'] = $this->db->count_all(‘yourTableName’);
      $config['per_page'] =10;

      $this->load->library(‘pagination’);
      $this->pagination->initialize($config);

      //load the model and get results
      $this->load->model(‘admin/user_model’);

      $data['dloop'] = $this->user_model->getContentPage($config['per_page'],(int)$this->uri->segment(4));

      echo $this->load->view(“admin/news_view”,$data,true);

      in MODEL:

      function getContentPage($num, $offset){

      $sql_q=”SELECT * FROM table JOIN table2 WHERE table.id = table2.id_table2
      ORDER BY table.id
      Limit $offset,$num”;

      $query = $this->db->query($sql_q);
      return $query->result_array();
      }

      in View :

      pagination->create_links(); ?>

  • Khalil

    Dear Jeffrey Way,

    Please give me solution…

    CI Pagination Problem:

    my URL is http://www.myDomain.com/myController/myFunction/passingRowID/paggination

    pagination is not working correctly with “segment(4)”

    waiting for reply…

    Thank you.

  • Newicz

    Hi guys!

    I have question about table library i have something like this:

    $this -> db -> select (‘id, title, made_date, auth, menu_item’);
    $query = $this -> db -> get (‘pages’);
    return $this -> table -> generate($query);

    And it is working, it gives me a table with pages in database, but i would like to add 2 columns (edit and delete) how can i do it, can anyone help me?

    thanks in advise!

  • http://newdailyblog.blogspot.com Tahsin Hasan

    Hello,

    see how to use page specific layout design for codeigniter on http://newdailyblog.blogspot.com/2010/06/creating-page-specific-layout-in.html. Thanks.

  • adelle

    That is a great tut .
    But i have a question , what should i do if i wanted to make the info inside the table dynamic such putting an anchor link ?

  • Roee A.

    Whenever your code is inside a folder inside the controller, the pagination will make some problems recognizing on which page you are.
    use $config['uri_segment] = X if the segment is not 3. :)
    It solved me a lot of problems.

    • Cameron Malek

      Thank you so much! I’ve been looking for a solution to this problem for 30 minutes now. You’ve saved me so much time!

  • ci9

    Another very nice Tuts again by Jef_way.

    I love you, i love you Jef you are my idol. LOL :)

    -ci9

  • Skot

    I have greatly enjoyed these tutorials on Codeigniter, however, I am unable to view the video for this tutorial, Day 7. I have attempted it via online, download, and iTunes to no avail. The download leads me to the Screencast site, but states that the page is 404. Please help.

    • Chike

      Scott, I had the same problems here & at iTunes — but a copy of the video plays just fine on blip.tv:

      http://blip.tv/file/2670407

  • http://www.bloghispano.com.ar Alfredo

    The first link is not available….the flv version function good.

  • Side

    Is it just for me or the video isn’t loading for others too?

    I waited for like 5 mins to load and nothing, why?

    Or if its possible could you please re upload it?

    i would be really happy, ty

  • http://matthewheyes.com Matt

    Hi Jeff,

    Why did you use jQuery to apply the table striping? Any reason not to use straight CSS?

    tr:nth-child(odd)

    - Awesome tutorials tho BTW. I’m loving them! Looking forward to some help with CI and shopping cart payment gateways.

    Thanks again,

    Matt

  • Rohit

    Hi Jeff & Burak,
    Thanks a lot for such a great videos! I am a web developer newbie. I had never heard of codeigniter before. After watching only few of your videos, I could write simple code.

    one question -what is that tool which you use for writing CSS with live preview?

  • meltz

    I learnt how to create web application from scratch just by watching this codeigniter series, before this I have 0 knowledge about codeigniter framework.

    Really love your tutorials, the screencast are so simple and clear. Keep up the good work !