HTML Table Class

The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table.

Loading an HTML Table Class

Like other libraries, we must load a table class in the Controller file by using the following syntax.

Syntax

Class Reference

There are various class references available in the Html table class:

  1. $function = NULL

A $function enables you to use a basic PHP function or a valid array object that can be applied to all cell data.

  • generate():As the name suggests, a generate() function is used to create a table that contains table data. It also allows you to pass an optional parameter that can be an array or a database result sets.

Syntax

$table_data: It defines the data that you want to show with table rows.

Example: Create a Html_controller.php controller file and save it to the following path application/controller/Html_controller.php folder. After that, write the following program in the controller file.

Html_controller.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Html_controller/useFunction; it shows the output as shown below.

HTML Table Class
  • set_caption(): A set_caption() function is used to show captions on a table.

Syntax

$caption: It is a variable that contains a string.

  • set_heading(): As the name defines, it is used to create a table heading by passing an array or multiple strings as a parameter to the set_heading() function.

Syntax

$arg: In this field, you can pass an array or string that contains table titles.

Example: $this->table->set_heading(‘Name’, ‘Roll_no’, ‘College’);

Using an array: $this->table->set_heading(array(‘Name’, Roll_no’, ‘College’);

  • add_row(): An add_row() function that allows you to create a row in your table by passing row values.

Syntax

$args: It consists of an array or multiple strings containing row values.

If you want to set multiple attributes in a single cell, you can use an associative array. And this attribute includes a key-value pairs for that cell.

Example: Create a Html_controller.php controller file and save it to the following path application/controller/Html_controller.php folder. After that, write the following program in the controller file.

Html_controller.php

Example: Create a data_view.php view file and save it to the following path application/views/data_view.php folder. After that, write the following program in the view file.

data_view.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Html_controller/setCaption function; It shows the output as shown below.

HTML Table Class
  • make_columns(): A make_columns() function is used to create a multidimensional array with an equal depth using a one-dimensional array and number of columns as the input to be displayed in a table.

Syntax

It has two parameters:

$array: It defines an array that contains multiple rows’ data as an input.

$col_limit: It shows the number of columns in the table.

Example: Create a Html_controller.php at application/controller/ folder and write the following program in the Html_controller.php file

Html_controller.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Html_controller/col function; it shows the output as shown below.

HTML Table Class
  • set_template(): A set_template() function is used to set an additional attributes in a template or table.

Syntax

$template: It defines an associative array that contains a template value.

Example:

  • set_empty(): It is used to set a default value in any table cells such as, set a non-breaking space:

Syntax

$value: It contains a value that is placed in an empty cell.

Example:

  • clear(): As the name suggests, it is used to clear table headings and row data. And if you want to show multiple tables with different data, you can call a clear() function after each generated table to clear the previous information of the table.

Example:

Create a table from the database query:

In this topic, we will learn how we can create a table from the database query result. The database query() function is used to fetch all stored data from the database table and produces a heading based table that you have defined in the set_heading() function.

Syntax

Example: To create an html table from the database records:

While creating an Html table, you have to ensure that your database connection should be connected with your Codeigniter application, and there must be some records in a database table. Suppose there is a ‘users’ table in the database to create Html table.

Create an Html_controller.php file at the application/controller/ folder and write the following program in the controller file.

Html_controller.php

Similarly, create a data_view.php file at the application/views/ folder and write the following program in the view file.

data_view.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Html_controller/data function; it shows the output as shown below.

HTML Table Class

Pin It on Pinterest

Share This