CodeIgniter from Scratch: Displaying & Sorting Tabular Data

CodeIgniter from Scratch: Displaying & Sorting Tabular Data

Tutorial Details
  • Topic: CodeIgniter
  • Difficulty: Intermediate
  • Tutorial Format: Video

In today’s video tutorial, we are going to use CodeIgniter to pull information from a database and display it in a sortable table structure. This is a quite common task, especially inside admin areas, for displaying database records. We’ll be utilizing a few different utilities, such as the active records library and pagination.


Catch Up


Day 16: Displaying & Sorting Tabular Data

Day 16: Displaying & Sorting Tabular Data

Thanks for watching! Any questions/thoughts?

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://eyoosuf.com Yoosuf

    these things can be done with javascript, but good to know :)

    • http://twitter.com/nine_L Lenin

      Yes that can be done with JS but if thats a huge amount of data then the page load would be higher too. Unless you’d use Ajax to load data.

      • http://odadia.com Matthew V

        I agree. When you have huge amounts of data, server-side sorting is deemed better. See Google and eBay etc.

  • mikky

    You guys are like the best thing since the invention of slice bread.

    • Eduardo Barros

      +1

  • http://www.creolab.hr Boris Strahija

    Just a little tip, insted of using the COUNT(*) you can use this:
    $this->db->count_all();

    • http://- Nick

      Why is that better?

    • http://www.phpandstuff.com/ Burak
      Author

      Only if you want to count all rows in a table. Once you start adding search functionality, and using where clause (which should be on the next episode), you can’t really use that anymore.

      • AGM

        In this case is better count(1) that count(*). Is a optimization in SQL world. Thank Burak (and Jeffrey too)

      • Robert

        CodeIgniter has its own num_rows() function that you can use – $this->db->query()->num_rows(); (http://codeigniter.com/user_guide/database/results.html). It gives you access to the number of rows returned by your query without requiring anything special. It should be called after get(), but before result().

  • Стоян Киров

    Great tutorial! I’m with mikky :)

  • Andrew

    And what if this form also needs to be also searchable? What would then url`s look like?

  • Raul

    Hey guys.

    I really like these tutorials, but … these are for CI newbies, but for them are GREAT.
    What i want to ask you is: why all tutorials use the view library and not the parser ? I can give you examples or make a video about it. I think the Parser class is better for programming.

    What do you think ? What’s the best ? The View or the Parser ?

    Thanks,

    Raul

  • http://www.buddyforever.com Khalil

    Great tutorial, Thank you!

    • http://www.buddyforever.com Khalil

      Dear Burak Guzel,

      I really want to learn about Codeigniter’s URI-Routing and Output Class. Please let us learn with your great “CodeIgniter from Scratch” series.

      I am requesting you please give us your screen-cast. I will be waiting for your reply – Thank you.

  • MIkhail
  • http://vidarvestnes.blogspot.com Vidar Vestnes

    Thanks Burak! Big fan of your tutts!

  • http://www.sz-media.org Sz-Media

    Hey Burak, another great Tutorial! Can you explain the templating with code igniter in your next tutorial?
    I mean, when you have classes like login and latest news. how to integrate them to the main site with header, navi, content and a sidebar with login and latest news ?! thats the point i got stuck with :(

  • http://www.tastybytes.net Brian

    Any chance of a non-video version, even if its just the code snippets?

  • http://www.taloco.com.br/site Brayan

    Any link to download the video ?

    Thanks.

  • erminio ottone

    wow! thanks :) love your tuts!

    ….ps: please please a tut on php debugging would be great :) xdebug have a good integration with aptana :)

  • http://www.bis2bis.com.br gerson kakuda

    Nice tutorial, I’m always following, thanks!

  • Sapono

    Please make tutorial about templating using codeigniter

  • http://itvillage.site11.com IT Village

    please give some tutorial about cakephp. by the way nice post. I really enjoyed it.

  • http://www.twitter.com/mamunabms Abdullah Al Mamun

    Thanks! :-)
    The series is really rocking.
    I hope a detail tutorial on SAAS application using CodeIgniter will come next.

  • http://iamhenry.me Henry

    Thanks for taking the time to produce a quality tutorial.

    Is there any way to download all of these for offline viewing?

    -Henry

  • Brad

    Excellent tut Burak, this one gets deeper into the mist. Will the code be available for download? I hate to be the only one NOT begging for something :)

  • http://www.carlwalker.me Carl Walker

    Excellent tutorial, very well presented and covering everything I wanted to know about pagination with table data display!
    Thank you for your hard work.

  • http://www.jsxtech.com Jaspal Singh

    Great tutorial on CodeIgniter.
    Please upload the code as attachment for downloading.
    Thanks for sharing.

  • http://www.hastishah.com Hastimal Shah

    Awesome tutorial..
    Any one can begin with this series to learn CI easily..
    Thanks for posting

  • Kestutis

    Great series, Burak.
    For this task there is also a great plugin on http://www.datatables.net/. And it’s working well with codeigniter (I tried by myself).

  • Natrium

    where can I find the sourcefiles?

    • http://www.phpandstuff.com Burak Guzel
      Author

      Click the button that says “Download Source Files” on top of the article.

  • http://www.webcorsa.com webcorsa

    hi Burak Guzel

    excellent but i want to download this video if possible then post like thru reply

    Thanks Burak

  • Lucho

    Quick tip: you can use $ $q->row() instead of using a tmp variable to get the first row from $q->result().
    So it can be just $ret['num_rows'] = $q->row()->count;

  • Omid

    It seems some links of this tutorial are broken !
    Could you please check it ?

  • irfan

    great series.

    does jeffery has stop the php series or not it is pending

  • http://www.erkekoyunlari.org erkek oyunu oyna

    I want to download this video if possible then post like thru reply .. Thanks…

  • erminio ottone

    @burak is that aptana studio 3.0 beta? how do you feel it? better/bug free than 2.0 ? (i saw you switched to netbeans for some time than returned to aptana, that means in someway you still prefer aptana)

    • http://www.phpandstuff.com/ Burak
      Author

      Nah, it’s still Aptana 1.5
      I use it because I like the FTP sync manager, and I need it for a lot of my projects.

      I will give Aptana 3 beta a try again, since they keep updating it. Last time I tried it, PHP wasn’t fully implemented yet.

  • http://www.tenaxtechnologies.com belarus software company

    What if this form also needs to be also searchable? What would then url`s look like?

    • http://www.phpandstuff.com/ Burak
      Author

      That’s the next episode.

      • http://www.padsbogota.com Andy

        Burak,

        Thanks for the great tutorials. When can we expect the next episode? I could really do with it about now! ;)

  • http://www.tenaxtechnologies.com software company for startups

    Excellent tutorial, very well presented.
    Thanks.

  • Brad

    Hey Burak,

    In the model you have on your line 8

    “$sort_by = (in_array($sort_order, $sort_columns)) ? $sort_by : ‘title;”

    Shouldn’t that be

    “$sort_by = ( ! in_array($sort_order, $sort_columns)) ? $sort_by : ‘title’;”?

    If you say its “in_array” you will sort all columns based on the title in my use with this tut.

    • http://www.phpandstuff.com Burak Guzel
      Author

      This is the line:
      $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : ‘title’;

      Basically $sort_columns contains all acceptable values for $sort_by. If $sort_by is in that array, it is assigned back to itself (so it isn’t changed). Otherwise, it is assigned ‘title’, because that is the default sort_by value.

      • Brad

        I see, when I used it that way it would only sort according to the title column, all sort links worked but they would not sort properly(used the title as the lead sort in all rows). So I changed it to what I wrote and it worked fine. Sorry!

        One other question, when you use FALSE and remove the backticks on a query, are you sacrificing security? Model like 17 on my file

      • http://www.phpandstuff.com/ Burak
        Author

        There is no user input variable in that select clause, so it’s not a security risk.

  • http://eyoosuf.com Yoosuf

    to be honest, when you guys are posting the Code, seriously we never work or Try out, but when without code we do search and find for more resource, so always better not the add the code along with the video.

  • daniel

    Excelent video, thanks! What about Kohana framework tutorials?

    • http://www.phpandstuff.com/ Burak
      Author

      I would like to learn Kohana too, but I keep hearing the documentation is a bit lacking.

  • Mr-h

    thank you burak
    but i think it will be nice if you show us how to use jquery with codeigniter

  • http://www.tasarimda.net cagatay

    you used this lines

    $config['base_url'] = site_url(“films/display/$sort_by/$sort_order”);
    $config['uri_segment'] = 5;

    i want to know about offset

    when i tried this url “films/display/20″ will i get error?

    thanks burak

  • http://www.stuudio.sk roarbb

    Hi Burak. This tutorials are really great. Thanks.

    What about make tutorial about template system for codeigniter? Maybe some template library or so. It will be great and usefull for many of us. Thx.

  • http://www.macoyunlari.com maç oyna

    Thanks.. I think it will be nice if you show us how to use jquery with codeigniter..

  • Flavio

    Can you do debugging of CodeIgniter in Aptana ?

    Debugging could be a GREAT addition to this series.

  • http://www.kizoyun.org kız makyaj oyunu

    Great video, thanks!

  • http://www.mediabeast.co.nz myles.b

    Can you post the database fields??

  • http://www.mediabeast.co.nz myles.b

    Never mind ;)

  • Petar

    What about the next day (day 17)?

  • http://www.hotcoffee.be Pellens

    Maybe working with different languages would be a great next topic for a CI-tutorial?

    Good work!

  • bati

    I agree with Pellens on this one. I think multi-language CI application would be much appreciated by CI followers… awesome series by the way…

  • Lars

    Great tutorials!
    Could you please write a tutorial about template system for codeigniter?

    i need a example how i create a template for using

    Header and Footer and Menu on every page.

  • Ross

    Burak, thanks a million. Seriously good stuff. Keep it coming!

  • Dan LaManna

    Gotta say these tutorials are absolutely great, I learned alot from knowing absolutely nothing with the framework. However one thing I’ve yet to become clear on is how to actually make a full fledged website with codeIgniter, what models/controllers I would use, etc.

  • Ross

    Two tips.

    1: If you are sorting on a mysql enum field, the sort clause will not return correctly sorted records UNLESS you have created the enum field with the options already in alphabetical (and I assume, numeric) order. I had this problem, changed my enum order, and it works fine.

    2: to use the solid up or down triangle as Burak does, you can use the unicode in your CSS.

    .sort_asc:after{
    content:”\25B2″;/** unicode arial for up arrow **/
    }
    .sort_desc:after{
    content: “\25BC”;/** unicode arial for down arrow **/
    }

    I had trouble copying and pasting from the XP character map, but the above works fine.