php代码在codeigniter中显示文本框内的选定行数据
问题描述:
I have a single form for add ,view update and delete.
My page contain table with Edit and delete link.
I need when I click on edit link then it will show the selected row data inside text box.
How can I populate the selected row data and display inside a text box?
This is all in codeIgnitter.
plz see the attached screen shots here for refrence of my view The code is below:
Controller
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);
}
form and list view 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>
答
Don't Write query directly in controller. This is bad use of MVC. You can write your query in model file.
Your code not fetching data from database
Controller
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);
}
Model
function get_data($id)
{
$this->db->where('id',$id);
$query=$this->db->get('categort_tbl');
return $query->row();// fetch row and return from model
}
Views
<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>