The table class provides multiple functions that allow you to automatically generate HTML tables from arrays or database results.
Initialization class
Like other codeigniter classes, use the $ this-> load-> library function in the Controller to initialize the table class:
$this->load->library('table');
Once loaded, you can create an instance of a table store object as follows: $ this-> table
Example
This example shows how to use a multi-dimensional array to automatically generate a table.
Note: The first index of the array will become the header (or you can use the set_heading () function to customize the header ).
$this->load->library('table');$data = array( array('Name', 'Color', 'Size'), array('Fred', 'Blue', 'Small'), array('Mary', 'Red', 'Large'), array('John', 'Green', 'Medium') );echo $this->table->generate($data);
:
The following is an example of a table created from the database query structure.
The table class automatically generates the table title based on the table name (refer to the function described below, you can use the set_heading () function to set your own title ).
$this->load->library('table');$query = $this->db->query("SELECT * FROM my_table");echo $this->table->generate($query);
Effect:
This example demonstrates how to use continuous parameters to create a table:
$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();
In this simple example, in addition to changing some parameters, an array is also used:
$this->load->library('table');$this->table->set_heading(array('Name', 'Color', 'Size'));$this->table->add_row(array('Fred', 'Blue', 'Small'));$this->table->add_row(array('Mary', 'Red', 'Large'));$this->table->add_row(array('John', 'Green', 'Medium'));echo $this->table->generate();
Modify the appearance of a table
The table class allows you to set a table template with the design orchestration you specified. Here is the template prototype:
$tmpl = array ( 'table_open' => '<table border="0" cellpadding="4" cellspacing="0">', 'heading_row_start' => '<tr>', 'heading_row_end' => '</tr>', 'heading_cell_start' => '<th>', 'heading_cell_end' => '</th>', 'row_start' => '<tr>', 'row_end' => '</tr>', 'cell_start' => '<td>', 'cell_end' => '</td>', 'row_alt_start' => '<tr>', 'row_alt_end' => '</tr>', 'cell_alt_start' => '<td>', 'cell_alt_end' => '</td>', 'table_close' => '</table>' );$this->table->set_template($tmpl);
Note: In this template, you will find two "row" Block settings. This allows you to create a line color or design the recurrence element of each line of data.
You do not have to submit all templates. If you only want to change a part of the orchestration, you can simply submit the elements of that part. In this example, only the table start label is changed:
$tmpl = array ( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );$this->table->set_template($tmpl);
Function reference:
$ This-> table-> Generate ()
Returns a string containing the generated table. Accept an optional parameter, which can be an array or result object obtained from the database.
$ This-> table-> set_caption ()
Allow you to add a title to the table
$this->table->set_caption('Colors');
$ This-> table-> set_heading ()
You can set the table header. You can submit an array or separate parameters:
$this->table->set_heading('Name', 'Color', 'Size');$this->table->set_heading(array('Name', 'Color', 'Size'));
$ This-> table-> add_row ()
You can add a row to your table. You can submit an array or separate parameters:
$this->table->add_row('Blue', 'Red', 'Green');$this->table->add_row(array('Blue', 'Red', 'Green'));
If you want to set the attribute of a cell separately, you can use an associated array.
The join key 'data' defines the data of this cell.
Other key-value pairs key => Val will be added as key = 'val 'as the attribute of the Cell:
$ Cell = array ('data' => 'blue', 'class' => 'highlight', 'colspan '=> 2 ); $ this-> table-> add_row ($ cell, 'red', 'green '); // generate // <TD class = 'highlight' colspan = '2'> blue </TD> <TD> Red </TD> <TD> green </TD>
$ This-> table-> make_columns ()
This function uses a one-dimensional array as the input to create a two-dimensional array with the same depth as the number of columns. This function sorts and displays a single array with multiple elements based on the number of columns in the table. See the following 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);
The generated HTML code is as follows:
<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>
$ This-> table-> set_template ()
Allow you to set your template. You can submit the entire template or a local template.
$tmpl = array ( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );$this->table->set_template($tmpl);$this->table->set_empty()
You can set a default value to display cells with empty content in the table. For example, you can set a non-breaking space to prevent broken table borders ):
$this->table->set_empty(" ");$this->table->clear()
This allows you to clear the table header and row data. If you need to display multiple tables with different data, you need to call this function after each table is generated to clear the previous table information. For 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();$this->table->function
You can specify a local PHP method or an effective method to apply to an array object of data in all cells.
$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 preceding example, the data in all cells can be escaped in HTML using the htmlspecialchars () method of PHP. The result is as follows:
<td>Fred</td><td><strong>Blue</strong></td><td>Small</td>