Welcome Guest, Not a member yet? Register   Sign In
HTML Table Class - add custom column to data selected from DB. Possible?
#1

[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.
#2

[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
#3

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




Theme © iAndrew 2016 - Forum software by © MyBB