Codeigniter HTML Table Class Library


Codeigniter HTML Table Class Library -We can load html table class library like this $this->load->library(‘table’);. This library provides various functions that are used to enable you to auto generate html tables from array or database result set. Here in this tutorial, we are going to explain how to use html table class library.


Codeigniter html table class library

Let us understand how html table class library works in codeigniter with examples.

Load html table class library

First load html table class library to use its functions, this library can be loaded simply as below-

How to load html table class library:

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

Class reference:-

There are following references available in html table class library. Now we will explain.

1. Function.

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

$function = NULL

Example of Function class reference.

Here is simple demo of function class reference.

Controller Code:-

Syntax of function class reference.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class table_controller extends CI_Controller 
{
public function tablecreate()
{
$this->load->library('table');
$this->load->view('table_view');		
}
}
?>

View Code:-

Syntax of retrieving session data on view page.

<html>
<head>      
<title>CodeIgniter Html Table Example</title> 
</head>	
<body>
<?php
$this->table->set_heading('Name', 'Color', 'Size');
$this->table->add_row('Fred', '<strong>Blue</strong>', 'Small');
$this->table->function = 'htmlspecialchars()';
echo $this->table->generate();
?>
</body>	
</html>

Output will be look like this

Codeigniter HTML Table Class Library

2. Generate.

This function return a string containing the generated table.

generate([$table_data = NULL])
  • Parameters :
  • $table_data (mixed) : Data to populate the table rows with
  • Returns : HTML table
  • Returns type : String

3. Set caption.

This method give permit you to add a caption to the table.

set_caption($caption)
  • Parameters :
  • $caption (string) : Table caption
  • Returns : CI_Table instance (method chaining)
  • Returns type : CI_Table

4. Set heading.

This method give permit you to set table heading.

set_heading([$args = array()[...]])
  • Parameters :
  • $args (mixed) : An array or multiple strings containing the table column titles
  • Returns : CI_Table instance (method chaining)
  • Returns type : CI_Table

Example.

Here is simple demo of set heading class reference.

Controller page is already defined in above example.

View Code:-

Syntax of set heading html on view page.

<html>
<head>      
<title>CodeIgniter Html Table Example</title> 
</head>	
<body>
<?php
$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();
?>
</body>	
</html>

Output will be look like this

Codeigniter HTML Table Class Library

5. Add row.

This method is used to add a row in your table.

add_row([$args = array()[...]])
  • Parameters :
  • $args (mixed) : An array or multiple strings containing the row values
  • Returns : CI_Table instance (method chaining)
  • Returns type : CI_Table

6. Make columns.

This method takes one dimensional array and create multidimensional array.

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
  • Returns type : Array

Example.

Here is simple demo of make coloum reference.

View Code:-

Syntax of make coloum html on view page.

<html>
<head>      
<title>CodeIgniter Html Table Example</title> 
</head>	
<body>
<?php
$list = array('one', 'two', 'three', 'four', 'five', 'six',
 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');
$new_list = $this->table->make_columns($list, 3);
echo $this->table->generate($new_list);
?>
</body>	
</html>

Output will be look like this

Codeigniter HTML Table Class Library

7. Set template.

This method permit you to set your template.

set_template($template)
  • Parameters :
  • $template (array) : An associative array containing template values
  • Returns : TRUE on success, FALSE on failure
  • Returns type : Bool

8. Set empty.

This method is used to set non breaking space.

set_empty($value)
  • Parameters :
  • $value (mixed) : Value to put in empty cells
  • Returns : CI_Table instance
  • Returns type : CI_Table

9. Clear.

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.

clear()
  • Parameters :
  • Returns : CI_Table instance
  • Returns type : CI_Table

More Example about html table library.

Here is simple demo how to display table data from database.

View Code:-

Syntax of display database table on view page.

<html>
<head>      
<title>CodeIgniter Html Table Example</title> 
</head>	
<body>
<?php
$query = $this->db->query('SELECT * FROM example');
$result = $this->table->generate($query);
echo $result;
?>
</body>	
</html>

Output will be look like this

Codeigniter HTML Table Class Library

Advertisements

Add Comment

📖 Read More