From a1931ad87e8145f08dbf381ceab5bf88457b1a83 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 9 Oct 2006 04:17:38 +0000 Subject: --- user_guide/libraries/table.html | 138 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 2 deletions(-) (limited to 'user_guide/libraries') diff --git a/user_guide/libraries/table.html b/user_guide/libraries/table.html index f861a2c96..e80e53fd7 100644 --- a/user_guide/libraries/table.html +++ b/user_guide/libraries/table.html @@ -70,9 +70,143 @@ HTML Table Class

Like most other classes in Code Igniter, the Table class is initialized in your controller using the $this->load->library function:

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

Once loaded, the Trackback library object will be available using: $this->table

- +

Once loaded, the Table library object will be available using: $this->table

+ + +

Examples

+ +

Here is an example showing how you can create a table from a multi-dimensional array. +Note that the first array index will become the table heading.

+ + +$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); +
+ +

Here is an example of a table created from a database query result. The table class will automatically generate the +headings based on the table names. Or you can set your own headings using the set_heading() function described +in the function reference below.

+ + +$this->load->library('table');
+
+$query = $this->db->query("SELECT * FROM my_table");
+
+echo $this->table->generate($query); +
+ + +

Here is an example showing how you might create a table using discreet parameters:

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

Here is the same example, except instead of individual parameters, arrays are 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(); +
+ + +

Changing the Look of Your Table

+ +

The Table Class permits you to set a table template with which you can specify the design of your layout. 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); +
+ +

You'll notice there are two sets of "row" blocks in the template. These permit you to create alternating row colors or design elements that alternate with each +iteration of the row data.

+ +

You are NOT required to submit a complete template. If you only need to change parts of the layout you can simply submit those elements. +In this example, only the table opening tag is being 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. Accepts an optional parameter which can be an array or a database result object.

+ +

$this->table->set_heading()

+ +

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

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

$this->table->add_row()

+ +

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

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

$this->table->set_template()

+ +

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

+ +$tmpl = array ( 'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );
+ +
+$this->table->set_template($tmpl); +
-- cgit v1.2.3-24-g4f1b