Php 在html表中创建链接
我有以下代码来生成一个表Php 在html表中创建链接,php,codeigniter,html-table,Php,Codeigniter,Html 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
$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);
$query=$this->search\u employee->getAll($config['per\u page'],$this->uri->segment(3));
$this->table->set_heading('Name','Department');
$tmp=数组('table_open'=>'';
$this->table->set_模板($tmp);
$data['main_content']='display_professors';
$data['table']=$this->table->generate($query);
我的问题是如何在“名称”列下的每一行下创建一个链接,链接到查看\u employee/ID
我使用的是codeigniter。可能有更好的方法,但我只是循环查询行来创建表行:
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();
等等
更新:
根据您对这个答案的评论,我看不出这个方法是如何将html放入模型的,而不是您自己的示例。毕竟,无论哪种方式,您都将在视图中执行
或类似操作
如果您绝对想将两者分开,可以执行以下操作:
$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;
}
}
然后在视图中:
<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>
名称
部门