CodeIgniter Forums
HTML Table Class - add custom column to data selected from DB. Possible? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: HTML Table Class - add custom column to data selected from DB. Possible? (/showthread.php?tid=31275)



HTML Table Class - add custom column to data selected from DB. Possible? - El Forum - 06-12-2010

[eluser]Unknown[/eluser]
Hi,

I'm currently working with the HTML Table Class and creating a table from a query. This is working well.

I am attempting to add a 'custom' column to each row and don't know if this is possible. Basically the HTML table currently looks like this:

Code:
ID        Field 1        Field 2      
1         Law            Present      
2         Sociology      Absent        
...

What I would like to add it a 4th column which will include a link which will link to another method using the ID as part of the URI. For example:

Code:
ID        Field 1        Field 2        Custom?
1         Law            Present        "LINK1"
2         Sociology      Absent         "LINK2"
...

LINK1 would need to look something like this: http://example.com/class/method/ID.

Is this possible with the HTML Table Class or will I have to write my own function to get the desired result.

Many thanks in advance.


HTML Table Class - add custom column to data selected from DB. Possible? - El Forum - 06-12-2010

[eluser]pickupman[/eluser]
Try this:
Code:
$result = $query->result(); //save result from db query

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

$this->table->set_heading('ID', 'Field 1', 'Field 2', 'Link'); //Create columns

if($query->num_rows() > 0)
{
  foreach($result as $row)
  {
    $this->table->add_row($row->ID, $row->Field_1, $row->Field_2, anchor('class/method/',$row->ID, 'Link ' .$row->ID)); //Add each result row into table
  }
}else{
   $this->table->add_row('No results found','','','');
}

echo $this->table->generate();  //Display table



HTML Table Class - add custom column to data selected from DB. Possible? - El Forum - 06-13-2010

[eluser]Unknown[/eluser]
Thanks for your advice pickupman - worked perfectly.