We can create table using HTML table tag. But in CodeIgniter, there is a Table Class to create table. It is not mandatory to use this Table Class, we can use simple HTML table tag also.
This Table Class provides facility of creating table from arrays or database query result set.
To initialize Table Class, we have to write following statement in the controller class:
Now, we can access Table Class library object using $this->table.
We can create table using multi-dimensional array. First array index becomes table heading. We can also make table heading using set_heading() method.
Example:
OUTPUT:
Above example generates table with 4 rows and 3 columns.
We can direct generate table from database query result.
Below is an example of table generation from database query result.
Above code creates table from database table 'my_table'.
We can add row to the table using add_row() function and we can set heading using set_heading() function. See below code for the same:
We can also use array like below code:
This function creates multi-dimensional array with depth equal to the number of columns desired(second argument). This function takes two argument one is one-dimensional array and second is number of columns required in the table.
Example
OUTPUT:
$this->table->generate()
This function is already used in above example. generate() function generates/creates table. To print, use echo statement.
$this->table->set_caption()
This function is used to set caption of the table.
$this->table->set_heading()
This function is also already used in above example. This function sets heading, like <th> tag in HTML.
$this->table->add_row()
This function creates row in the table. See above example, we already used add_row() function.
If we want to set attribute to particular cell, then we have to use associative array like below:
$this->table->make_columns()
Example of this function we already discussed. This function creates multi-dimensional array with desired number of columns.
$this->table->set_template()
We can create own template. See below example:
$this->table->set_empty()
Using this function, we can set default value for any table cells that are empty.
$this->table->clear()
This function clears table heading and row data.
If you want to create table using HTML <table> tag then click on following link:
Read How To Create Table In HTML
Conclusion:
CodeIgniter Table creation is very simple. But if you are not comfortable with this CodeIgniter Table Class then use simple table tag of HTML.
CodeIgniter Table Class Example |
This Table Class provides facility of creating table from arrays or database query result set.
1. How To Initialize Table Class?
To initialize Table Class, we have to write following statement in the controller class:
$this->load->library('table');
Now, we can access Table Class library object using $this->table.
2. Create Table From Array
We can create table using multi-dimensional array. First array index becomes table heading. We can also make table heading using set_heading() method.
Example:
$this->load->library('table');
$rows = array(
array('RollNo','Name','City'),
array('1','Jignesh','Rajkot'),
array('2','John','Washington'),
array('3','Mark','Canada')
);
echo $this->table->generate($rows);
$rows = array(
array('RollNo','Name','City'),
array('1','Jignesh','Rajkot'),
array('2','John','Washington'),
array('3','Mark','Canada')
);
echo $this->table->generate($rows);
OUTPUT:
RollNo | Name | City |
---|---|---|
1 | Jignesh | Rajkot |
2 | John | Washington |
3 | Mark | Canada |
Above example generates table with 4 rows and 3 columns.
3. Create Table From Database Query Result
We can direct generate table from database query result.
Below is an example of table generation from database query result.
$this->load->library('table');
$query = $this->db->query('SELECT * FROM my_table');
echo $this->table->generate($query);
$query = $this->db->query('SELECT * FROM my_table');
echo $this->table->generate($query);
Above code creates table from database table 'my_table'.
4. Example of add_row() and set_heading() Functions
We can add row to the table using add_row() function and we can set heading using set_heading() function. See below code for the same:
$this->load->library('table');
$this->table->set_heading('RollNo','Name','City');
$this->table->add_row('1','Jignesh','Rajkot');
$this->table->add_row('2','John','Washington');
$this->table->add_row('3','Mark','Canada');
echo $this->table->generate();
$this->table->set_heading('RollNo','Name','City');
$this->table->add_row('1','Jignesh','Rajkot');
$this->table->add_row('2','John','Washington');
$this->table->add_row('3','Mark','Canada');
echo $this->table->generate();
We can also use array like below code:
$this->table->add_row(array('1','Jignesh','Rajkot'));
5. Example of make_columns() Function
This function creates multi-dimensional array with depth equal to the number of columns desired(second argument). This function takes two argument one is one-dimensional array and second is number of columns required in the table.
Example
$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');
$newlist = $this->table->make_columns($list, 3);
$this->table->generate($newlist);
// Above code generates following code.
<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>
$newlist = $this->table->make_columns($list, 3);
$this->table->generate($newlist);
// Above code generates following code.
<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>
OUTPUT:
one | two | three |
four | five | six |
seven | eight | nine |
ten | eleven | twelve |
6. Function Reference of Table Class
$this->table->generate()
This function is already used in above example. generate() function generates/creates table. To print, use echo statement.
$this->table->set_caption()
This function is used to set caption of the table.
$this->table->set_caption('Student Details');
$this->table->set_heading()
This function is also already used in above example. This function sets heading, like <th> tag in HTML.
$this->table->add_row()
This function creates row in the table. See above example, we already used add_row() function.
If we want to set attribute to particular cell, then we have to use associative array like below:
$cell = array('data'=>'Red','class'=>'mystyle','colspan'=>2);
$this->table->add_row($cell,'Green','Blue');
// Above code generates following HTML code
<td class="mystyle" colspan="2">Red</td>
<td> Green </td>
<td> Blue </td>
$this->table->add_row($cell,'Green','Blue');
// Above code generates following HTML code
<td class="mystyle" colspan="2">Red</td>
<td> Green </td>
<td> Blue </td>
$this->table->make_columns()
Example of this function we already discussed. This function creates multi-dimensional array with desired number of columns.
$this->table->set_template()
We can create own template. See below example:
$tmpl = array('table_open'=>'<table border="1" cellpadding="2" cellspacing="1" class="mytable">');
$this->table->set_template($tmpl);
$this->table->set_template($tmpl);
$this->table->set_empty()
Using this function, we can set default value for any table cells that are empty.
$this->table->set_empty(" ");
$this->table->clear()
This function clears table heading and row data.
If you want to create table using HTML <table> tag then click on following link:
Read How To Create Table In HTML
Conclusion:
CodeIgniter Table creation is very simple. But if you are not comfortable with this CodeIgniter Table Class then use simple table tag of HTML.