Captions in HTML Table Class
After a recent request for this, I’m re-posting the code to generate table captions in Code Igniter that I originally posted in the forums. Using it is very simple:
$this->table->set_caption()
Adds a caption to your table. Here's the full example and code.
$this->load->library('table');
$this->table->set_heading('Name', 'Color', 'Size');
$this->table->add_row('Fred', 'Blue', 'Small');
$this->table->add_row('Mary', 'Red', 'Large');
$this->table->add_row('John', 'Green', 'Medium');
echo $this->table->set_caption('Orders by Person');
echo $this->table->generate();
gives us
<table>
<caption>Orders by Person</caption>
<tr>
...
</tr>
</table>
To add it, open up /system/libraries/Table.php, and modify it as such
// to CI_Table I added this var def'n at the top
var $caption = NULL;
// new function within CI_Table
/**
* Add a table caption
*
* @access public
* @param mixed
* @return void
*/
function set_caption($caption)
{
$this->caption = $caption;
}
// within the generate function
// Build the table!
$out = $this->template['table_open'];
$out .= $this->newline;
// these lines are new
if ($this->caption)
{
$out .= $this->newline;
$out .= '<caption>' . $this->caption . '</caption>';
$out .= $this->newline;
}
// end new lines
// Is there a table heading to display?
if (count($this->heading) > 0)
...
I’ve made a fully patched Table.php file available for download. Just replace your current Table with it.

Derek wrote on
I’m just thinking about this. I wrote it before Code Igniter would let you extend the core libraries, but if you wanted to use it without needing to hack the CI core, you could just extend the native library.
Truthfully, that probably makes more sense.