当前,我的视图类中有一个表,它正在使用foreach循环从后端填充数据。我为表的每一行都有一个编辑单元格,当单击id为1的单元格时,我想用它将用户重定向到类似contacts/edit/1
的页面,该页面应该显示id为1用户的所有数据。我使用以下代码来实现这一点:
视图类(index.php(:
<?php
foreach($records as $row ){
?>
<tr>
<td><?= $row->refno ?></td>
<td><?= $row->display_name ?></td>
<td><a href="contacts/edit/'.$row->id.'">
<span class="sr-only">edit</span></a>
</td>
<td></td>
</tr>
控制器类别:
public function lists($type='')
{
$main['records']=$this->contacts_model->get_records();
$main['page'] = 'crm/contacts/index';
$this->load->view('crm/index',$main);
}
public function edit($slug='')
{
$main['page'] = 'crm/contacts/edit';
$this->load->view('crm/index',$main);
}
型号类别:
function get_records(){
$this->db->select("*");
$this->db->from("contacts");
$this->db->where("status='Y'");
$query = $this->db->get();
return $query->result();
}
// I want another method here that will fetch the details as per the id that was selected
因此,我有两个问题,一是我的<a>
标记在我将其设置为contacts/edit/'.$row->id.'
后不会将我带到相应的id,二是我如何在联系人/编辑视图类中显示id为1的人的详细信息。
解决第一个问题非常简单。你犯了一个简单的语法错误:
错误:
<?php
...
<td><a href="contacts/edit/'.$row->id.'">
...
正确:
<?php
...
<td><a href="<?= 'contacts/edit/'. $row->id ?>">
...
注意:只有在php.ini中启用了short_open_tags标志时,短格式echo(<?= "some text or variables" ?>
(才有效!
对于第二个问题,您需要在控制器中创建一个其他功能,用于收集和显示具有基于ID的数据的视图。