Codeigniter Tables 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.



<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Books extends CI_Controller{
    function __construct(){
        parent::__construct();
    }
    public function index(){
        // Load the tables library
        $this->load->library('table');
    }
}
?>
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:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Books extends CI_Controller{
    function __construct(){
        parent::__construct();
    }
    public function index(){
        // Load the tables library
        $this->load->library('table');
        // Query the database and get results
        $data['books'] = $this->db->get('books');
    }
}
?>
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

<html>
<head>
<title>Jotorres Table class example</title>
</head>
<body>
    <div id='results'>
    <?php echo $this->table->generate($books); ?>
    </div>
</body>
</html>
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:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Books extends CI_Controller{
    function __construct(){
        parent::__construct();
    }
    public function index(){
        // Load the tables library
        $this->load->library('table');
        // Query the database and get results
        $data['books'] = $this->db->get('books');
        // Load the view and send the results
        $this->load->view('books_view', $data);
    }
}
?>
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:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Books extends CI_Controller{
    function __construct(){
        parent::__construct();
    }
    public function index(){
        // Load the tables library
        $this->load->library('table');
        // Query the database and get results
        $data['books'] = $this->db->get('books');
        // Create custom headers
        $header = array('Book ID', 'Book Name', 'Book Description', 'Book Author');
        // Set the headings
        $this->table->set_heading($header);
        // Load the view and send the results
        $this->load->view('books_view', $data);
    }
}
?>
Here you have it folks. It is plain and simple, to use codeigniter tables class. 


No comments:

Post a Comment