Reputation: 1
I have the following code to generate a table
$query = $this->search_employee->getAll($config['per_page'], $this->uri->segment(3));
$this->table->set_heading('Name', 'Department');
$tmp = array ( 'table_open' => '<table border="0" cellpadding="2" cellspacing="1" width="70%">' );
$this->table->set_template($tmp);
$data['main_content'] = 'display_professors';
$data['table'] = $this->table->generate($query);
My question is how could I create a link under to each row under the Name column that links to view_employee/ID
I am using codeigniter's table class.
Upvotes: 0
Views: 685
Reputation: 304
There might be a better way to do this, but I simply loop through query rows to create table rows:
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$this->table->add_row(anchor('view_employee/ID/' . $row->employee_id, $row->employee_name), $row->employee_department);
}
}
$data['table'] = $this->table->generate();
etc...
UPDATE:
Per your comment to this answer, I don't see how this method is anymore putting html into the model than your own example. After all, either way all you'll do in the view is <?= $table; ?>
, or similar.
If you absolutely want to separate the two, you could do:
$data['rows'] = array();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$this_row = array();
$this_row['link'] = site_url('view_employee/ID/' . $row->employee_id);
$this_row['employee_name'] = $row->employee_name;
$this_row['employee_department'] = $row->employee_department;
$data['rows'][] = $this_row;
}
}
and then in the view:
<table border="0" cellpadding="2" cellspacing="1" width="70%">
<tr>
<th>Name</th>
<th>Department</th>
</tr>
<? foreach($rows as $row): ?>
<tr>
<td><a href="<?= $row['link']; ?>"><?= $row['employee_name']; ?></a></td>
<td><?= $row['employee_department']; ?></td>
</tr>
<? endforeach; ?>
</table>
Upvotes: 2