CodeIgniter table class: add a link from the generated cell

I use a table class that automatically generates a table for me from an array of data extracted from my database.

Model

function get_reports_by_user_id($userid) { return $this->db->get_where('ss2_report',array('userid' => $userid))->result_array(); } 

controller

 function index() { echo $this->table->generate($this->mymodel->get_reports_by_user_id('1234')); } 

Ultimately, the controller will be moved to view mode when it is running. This creates a table just fine, but I would like to add a link to the field. For example, an id column that will allow me to link to a data page for that report identifier only. I know that I can simply display the table in the old-fashioned way manually. Then I can add any links that I need, but I would like to use auto-generation as much as possible. There must be a way to do something as general as binding a table cell. Does anyone have any ideas?

EDIT

The Java PHP user has it basically right below. Here is the code that makes it work:

 function get_reports_by_user_id($userid) { $rows = $this->db->get_where('ss2_report',array('userid' => $userid))->result_array(); foreach ($rows as $count => $row) { $rows[$count]['id'] = anchor('report/'.$row['id'],$row['id']); } return $rows; } 

I just needed to replace the value in the original array with the text version.

+4
source share
2 answers

The only way, in the get_reports_by_user_id() function, you would skip all the results and add the <a href> tag to the identifiers. Something like that:

 function get_reports_by_user_id($userid) { $rows=$this->db->get_where('ss2_report',array('userid' => $userid))->result_array(); foreach ($rows as $row) { $row->id=anchor('site.com/some_controller/some_function/'.$row->id,$row->id); } return $rows; } 

I don't use the CodeIgniter database library, so I'm not sure what format it returns $rows , but the code above should give you a general idea of ​​what you need to do.

+7
source

One idea might be to do something like.

 foreach ($row in $this->mymodel->get_reports_by_user_id('1234')) { $row->id = anchor(site_url(array('report', 'user', $row->id)), $row->id); $this->table->add_row($row); } $this->table->generate(); 
+1
source

All Articles