February 14, 2012 CodeIgniter Image

Codeigniter Tables class

Codeigniter HTML Table class

Today I want to talk about an interesting class that is built within codeigniter. This class consists of just creating an HTML Table, but except, you do it with ease, and I am going to show you how.

In this example, we are going to assume we have a database connection, and within that database, we have a table called books. Let’s create a simple controller, that will grab the information that is within the ‘books’ table, and display it to the user.

In our controller named books.php, we will call the parent construct as always, (just for good programming habits). Then we create an index function. Remember, codeigniter will look for a function called index if no method is specified within the URL. Within this index, we will load the tables library class.

After loading the library, we would need an array with all the data that will be displayed. In this particular case, we will use all table columns that are found in the table database. Since we already have a database connection, then all we need to do is query the database to grab the information. So, adding to our controller we see:

Now all we have to do is create a view, and call it from our controller. Within this view, we will ‘generate’ the table to display the results from the query. Let’s create the view first:

Filename: books_view.php

The variable $books, comes from the $data array in the controller. Finally, let’s load the view from our controller, while sending in the data that we found in our query:

The table that will be generated with this class will include headers. These headers are the column names in the database table. If you would like to have a set of custom headers, then that can be achieved adding the following to your controller: Add set_heading(), but first, we need to fill in an array of custom names for the header. Chek out how it’s done:

Here you have it folks. It is plain and simple, to use codeigniter tables class.

12 thoughts on “Codeigniter Tables class

  1. I’ve seen that class in the Libraries folder several times but have completely ignored it until now. It’s not documented in the CI user guide so I guess I figured it probably wouldn’t be there for too much longer.

    Thanks Jotorres.

    1. Hi Mark,

      It actually is documented. The problem is it’s just overlooked sometimes. Just goes to show how powerful a framework could be when we pay more attention to all the details it contains.

      Glad you liked it!

  2. I am learning CI and earlier i was having 15 lines to make my table with php foreach loop in my view.

    Now my view code is having single line in my view…Awesome..

    Thanks for the explanation..

    Tapan Thapa

  3. Just curious, why wouldn’t the database call

    $data[‘books’] = $this->db->get(‘books’);

    be in a model instead of the controller?

    1. Well, it’s just for the sake of this tutorial, to teach people how easy it can be. Of course, anyone using any MVC framework, is suggested that you do it the way it is meant to be, FAT Models, Skinny Controllers. 😀

  4. Thanks for the tutorial (and the pagination follow-up), it’s nicely done. However, I get an error when I follow the pagination links. Can you spare some advice?

    I have set-up my code like yours, but the table won’t load in the paginated view because of this error:
    Undefined property: CI_Loader::$table

    In the view, I have this code:
    echo $this->table->generate($stories);

    In the controller I have this code:
    $data[‘stories’] = $this->db->get(‘news’, $config[‘per_page’], $offset);

    Any advice on how to solve this?

      1. Hi, thanks very much for the reply. The table library was not the culprit, it was routes. There was no route to handle the variable passed by the third URI segment. I fixed it with the addition of a two new routes:

        $route[‘news/index/(:any)’] = ‘news/index/$1’;
        // to handle the offset
        $route[‘news/index’] = ‘news’;
        // to handle the first page link generated by CI.

        Thanks again for your response. I appreciate your time.

Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.