我有一个表单用于添加,查看更新和删除。
我的页面包含带有编辑和删除链接的表格。
我需要点击编辑链接,然后它会在文本框中显示所选的行数据。
如何填充选定的行数据并在文本框中显示?
这完全在codeIgnitter中。
plz see the attached screen shots here for refrence of my view 代码如下:
控制器
public function edit()
{
$id = $this->input->get('id');
$this->db->where('id',$id);
$data['query'] = $this->db->get('categort_tbl');
$data['id'] = $id;
$this->load->view('category', $data);
}
public function select()
{
$data['title'] = "Welcome to DB";
$data['results'] = $this->category_model->getAll();
$this->load->view('category',$data);
}
表单和列表视图 的 category.php
<form name="frm1" method="post" action="<?php echo base_url(); ?>category/save">
<table height="40px">
<tr>
<td width="9%">Category :</td>
<td width="21%"><input type="text" name="category" required="" size="40" value=""/></td>
<td width="48%"><input type="submit" class="submit_button" name="save" value="Save"/></td>
</tr>
</table>
</form>
</div>
<div id="page-wrap">
<table width="60%" border="1">
<tr>
<td>Category</td>
<td>Date</td>
<td colspan="2" style="text-align:center">Actions</td>
</tr>
<?php if(isset($results)){ foreach($results as $row) { ?>
<tr>
<td><?php echo $row->category; ?></td>
<td><?php echo $row->created; ?></td>
<td><?php echo anchor('category/edit?id='.$row->id,'Edit')?></td>
<td><?php echo anchor('category/delete?del='.$row->id,'Delete',array('onclick' => "return confirm('Do you want delete this record')"))?></td>
</tr>
<?php } } ?>
</table>
</div>
答案 0 :(得分:0)
不要直接在控制器中写入查询。这是MVC的坏用法。您可以在模型文件中编写查询。
您的代码无法从数据库中获取数据
<强>控制器强>
function edit()
{
$id = $this->input->get('id');
$this->db->where('id',$id);
$data['data'] = $this->model_name->get_data($id);// call your model file and pass result in data variable
$data['id'] = $id;
$data['title'] = "Welcome to DB";
$data['results'] = $this->category_model->getAll();
$this->load->view('category', $data);
}
<强>模型强>
function get_data($id)
{
$this->db->where('id',$id);
$query=$this->db->get('categort_tbl');
return $query->row();// fetch row and return from model
}
<强>视图强>
<tr>
<td width="9%">Category :</td>
<td width="21%"><input type="text" name="category" required="" size="40" value="<?php echo $data->category;?>"/></td>
<td width="48%"><input type="submit" class="submit_button" name="save" value="Save"/></td>
</tr>