CI_Table

class CI_Table

$function = NULL

Allows you to specify a native PHP function or a valid function array object to be applied to all cell data.

$this->load->library('table');

$this->table->set_heading('Name', 'Color', 'Size');
$this->table->add_row('Fred', '<strong>Blue</strong>', 'Small');

$this->table->function = 'htmlspecialchars';
echo $this->table->generate();

In the above example, all cell data would be ran through PHP’s htmlspecialchars() function, resulting in:

<td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td>
generate([$table_data = NULL])
Parameters:
  • $table_data (mixed) – Data to populate the table rows with
Returns:

HTML table

Return type:

string

Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object.

set_caption($caption)
Parameters:
  • $caption (string) – Table caption
Returns:

CI_Table instance (method chaining)

Return type:

CI_Table

Permits you to add a caption to the table.

$this->table->set_caption('Colors');
set_heading([$args = array()[, ...]])
Parameters:
  • $args (mixed) – An array or multiple strings containing the table column titles
Returns:

CI_Table instance (method chaining)

Return type:

CI_Table

Permits you to set the table heading. You can submit an array or discrete params:

$this->table->set_heading('Name', 'Color', 'Size');

$this->table->set_heading(array('Name', 'Color', 'Size'));
add_row([$args = array()[, ...]])
Parameters:
  • $args (mixed) – An array or multiple strings containing the row values
Returns:

CI_Table instance (method chaining)

Return type:

CI_Table

Permits you to add a row to your table. You can submit an array or discrete params:

$this->table->add_row('Blue', 'Red', 'Green');

$this->table->add_row(array('Blue', 'Red', 'Green'));

If you would like to set an individual cell’s tag attributes, you can use an associative array for that cell. The associative key data defines the cell’s data. Any other key => val pairs are added as key=’val’ attributes to the tag:

$cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2);
$this->table->add_row($cell, 'Red', 'Green');

// generates
// <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
make_columns([$array = array()[, $col_limit = 0]])
Parameters:
  • $array (array) – An array containing multiple rows’ data
  • $col_limit (int) – Count of columns in the table
Returns:

An array of HTML table columns

Return type:

array

This method takes a one-dimensional array as input and creates a multi-dimensional array with a depth equal to the number of columns desired. This allows a single array with many elements to be displayed in a table that has a fixed column count. Consider this example:

$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');

$new_list = $this->table->make_columns($list, 3);

$this->table->generate($new_list);

// Generates a table with this prototype

<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td>one</td><td>two</td><td>three</td>
</tr><tr>
<td>four</td><td>five</td><td>six</td>
</tr><tr>
<td>seven</td><td>eight</td><td>nine</td>
</tr><tr>
<td>ten</td><td>eleven</td><td>twelve</td></tr>
</table>
set_template($template)
Parameters:
  • $template (array) – An associative array containing template values
Returns:

TRUE on success, FALSE on failure

Return type:

bool

Permits you to set your template. You can submit a full or partial template.

$template = array(
        'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">'
);

$this->table->set_template($template);
set_empty($value)
Parameters:
  • $value (mixed) – Value to put in empty cells
Returns:

CI_Table instance (method chaining)

Return type:

CI_Table

Lets you set a default value for use in any table cells that are empty. You might, for example, set a non-breaking space:

$this->table->set_empty("&nbsp;");
clear()
Returns: CI_Table instance (method chaining)
Return type: CI_Table

Lets you clear the table heading and row data. If you need to show multiple tables with different data you should to call this method after each table has been generated to clear the previous table information. Example:

$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->generate();

$this->table->clear();

$this->table->set_heading('Name', 'Day', 'Delivery');
$this->table->add_row('Fred', 'Wednesday', 'Express');
$this->table->add_row('Mary', 'Monday', 'Air');
$this->table->add_row('John', 'Saturday', 'Overnight');

echo $this->table->generate();
doc_CodeIgniter
2016-10-15 16:31:55
Comments
Leave a Comment

Please login to continue.