×

HTML Table Class CodeIgniter

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

$this->load->library(‘table’);
 Once the class is loaded, you can use the table library object, as shown in the syntax:
 $this->table 

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.

$this->table->function = ‘htmlspecialchars()’;
  • 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

generate ( [$table_data = NULL ])

$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

<?php
 defined(' BASEPATH ') OR exit('No direct script access allowed');
 class Html_controller extends CI_Controller 
 {
     public function useFunction()
     {
         $this->load->library('table'); 
         echo "<title> Tutorial and Example </title>";
         $this->table->set_heading('Name', 'Qualification', 'Occupation', 'Country');
         $this->table->add_row('James', 'B.COM', 'Accountant', '<i>England</i>');
         $this->table->add_row('Josef', '<strong>M.TECH</strong>', 'IoS Developer', 'USA');
         $this->table->add_row('Allis', 'Zoology', 'Zoologist', 'USA'); 
         $this->table->add_row('John', 'A Level', 'Computer Teacher', '<b>France</b>');
         $this->table->function = 'htmlspecialchars()';
         echo $this->table->generate(); // it is used to generate a table with row data
     }
 }
 ?> 

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

set_caption ($caption)

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

$this->table->set_caption( ‘Tutorial and Example’ );
  • 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

set_heading ([ $arg = array() ])

$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

add_row ([ $args = array() [,.. ] ])

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

$this->table->add_row( ‘One’, ‘Two’, ‘Three’ );
 $this->table->add_row(array (‘One’, ‘Two’, ‘Three’, ‘Four’)); // using an array. 

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.

$cell = array(‘name’ => ‘Thomas’, ‘class’ => ‘highlight’, ‘colspan’ => 3);
 $this->table->add_row($cell, ‘Egypt’, ‘Adverb’); 

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

<?php
 defined(' BASEPATH ') OR exit('No direct script access allowed');
 class Html_controller extends CI_Controller 
 { 
      public function setCaption()
     {
         $this->load->library('table'); 
        $this->load->view('data_view'); // load data_view file
     }
 }
 ?> 

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

<html>
 <head>      
 <title> Tutorial and Example </title> 
 </head>           
 <body>
 <?php 
 $template = array('table_open' => '<table border = "3" >' );
 $this->table->set_template($template); 
 $this->table->set_caption('Table 2 - Student Details'); // It shows caption to the table.
 $this->table->set_heading(array('Roll_no.', 'Name', 'Email', 'Address')); // set_heading
 $this->table->add_row(array('01', 'Finch', 'Finch@yahoo.com', 'Australia'));
 $this->table->add_row(array('02', 'Lyra', 'lyra@gmail.com', 'UK'));
 $this->table->add_row(array('03', 'John', 'john@hcl.com', 'London')); 
 echo $this->table->generate();
 ?>
 </body>          
 </html> 

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

make_columns ([ $array = array() [, $col_limit = 0 ]])

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

<?php
 defined(' BASEPATH ') OR exit('No direct script access allowed');
 class Html_controller extends CI_Controller 
 { 
 public function col()
 {
     echo "<title> Tutorial and Example </title>";
     $this->load->library('table'); 
     $template = array(
         'table_open'  => '<table border="3" 
         bgcolor ="red" cellpadding="2" 
         cellspacing="1" class="mytable">'
     );
     $this->table->set_template($template);
     $num = array('One', 'Two', 'Three',  
     'Four', 'Five', 'January', 'February', 'March', 'April',
      'May', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday');
 $data = $this->table->make_columns($num, 3);
 echo $this->table->generate($data);
 } 
 }
 ?> 

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

set_template ($template)

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

Example:

$data = array(
             ‘table_open’ => ‘<table border =”3” cellpadding =”2” cellspacing=”2” class=”tutorial” bgcolor=”skyblue” >’ );
 $this->table->set_template($data); 
  • set_empty(): It is used to set a default value in any table cells such as, set a non-breaking space:

Syntax

set_empty($value)

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

Example:

$this->table->set_empty(“ ”);
  • 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.
$this->table->clear();

Example:

$this->table->set_heading('Name', 'Hobbies', 'Country');
 $this->table->add_row('Watson', 'Wednesday', 'USA');
 $this->table->add_row('Morris', 'Cricket', 'UK');
 echo $this->table->generate();
 $this->table->clear(); // It clears the previous data after each generated table 
 $this->table->set_heading(array('Name', 'Subject', 'Division'));
 $this->table->add_row(array('Angelina', 'English', 'First'));
 $this->table->add_row('Mary', 'Economics', 'Second');
 echo $this->table->generate(); 

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

$query = $this->db->query( ‘select * from tablename’ );
 echo $this->table->generate($query); 

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

<?php
 defined(' BASEPATH ') OR exit('No direct script access allowed');
 class Html_controller extends CI_Controller 
 { 
 public function data() 
     { 
       $this->load->library('table');
         $this->load->database(); // load database connection
         $this->load->view('data_view'); // load view file 
     }
 }
 ?> 

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

data_view.php

<html>
 <head>      
 <title> Tutorial and Example </title> 
 </head>           
 <body> 
 <?php 
 $template = array('table_open' => '<table border = "3" 
 bgcolor ="skyblue" cellspacing ="2" class ="table3"
 >' );
 $this->table->set_template($template);
 $this->table->set_caption('Table 3 - Student Details'); // set caption to the table
 $this->table->set_heading(array('Id', 'Name', 'Email', 'Course', 'Gender')); 
 $query = $this->db->query('SELECT * FROM users'); // users is the database table
 $result = $this->table->generate($query); // create database table
 echo $result;
 ?>
 </body>
 </html> 

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

Related Topics

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 minutes read.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

CodeIgniter Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

CodeIgniter URI Class

URI Class The CodeIgniter contains a URI class that is used to retrieve information from URI strings. It also provides suitability to get information from the re-routed segments. The system automatically initializes this...

6 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

CodeIgniter Security Helper

A Security helper file contains some predefined functions that are used to protect application from unauthorized access. Loading the Helper The following syntax is used to load the security helper in the CodeIgniter application. Syntax $this-> load->...

4 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.