我想添加简单的编辑功能,遗憾的是,我没有在tut中介绍,我希望有一个编辑按钮,可以打开表单并更新信息。
我从这里开始关注教程:
CRUD TUTORIAL
这是我的控制器:
<?php
class Site extends CI_Controller
{
function index()
{
$data = array();
if($query = $this->site_model->get_records())
{
$data['records'] = $query;
}
//$this->load->library('table');
$this->load->view('options_view',$data);
}
function create()
{
$data = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content')
);
$this->site_model->add_record($data);
$this->index();
}
function update()
{
$data = array (
'title' => 'My NEW UPDATED title',
'content' => 'NEW UPDATED content; UPDATED'
);
$this->site_model->update_record($data);
}
function delete()
{
$this->site_model->delete_row();
$this->index();
}
}
这是我的模特:
<?php
class Site_model extends CI_Model {
function get_records()
{
$query = $this->db->get('assets');
return $query->result();
// $query = $this->db->query('SELECT * FROM assets');
//echo $this->table->generate($query);
}
function add_record($data)
{
$this->db->insert('assets', $data);
return;
}
function update_record($data)
{
$this->db->where('id', 3);
$this->db->update('assets', $data);
}
function delete_row()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('assets');
}
}
以下是我的观点:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled</title>
<style type="text/css" media="screen">
label{display:block;}
</style>
</head>
<body>
<h2>Create</h2>
<?php echo form_open('site/create');?>
<p>
<label for="title">Title:</label>
<input type="text" name="title" id="title" />
</p>
<p>
<label for="content">Content:</label>
<input type="text" name="content" id="content" />
</p>
<p>
<input type="submit" value="submit" />
</p>
<?echo form_close(); ?>
<hr />
Read
<table>
<?php if(isset($records)) : foreach ($records as $row) : ?>
<tr>
<td>
<?php echo anchor("site/delete/$row->Id", $row->title); ?>
<td>
<td><?php echo $row->content; ?> </td>
<tr>
<td></td><td></td><td></td><td></td><td>edit</td>
</tr>
</tr>
<?php endforeach; ?>
</table>
<?php else : ?>
<h2>No records returned.</h2>
<?php endif; ?>
<hr />
<h2>Delete</h2>
<p>To sample the delete method, click on on of the headings above.
A delete query will automatically run.
</p>
</body>
</html>
答案 0 :(得分:0)
看起来似乎没有选项表单提交到视图中的编辑数据表。试试这个。
<强>控制器强>
<?php
class Site extends CI_Controller
{
function index(){
$data = array();
if($query = $this->site_model->get_records()){
$data['records'] = $query;
}
$arr = $_SERVER['REQUEST_URI'];
if (preg_match('#[0-9]#',$arr)){
$questionmark = explode('?', $arr);
$number = $questionmark[1];
if(is_numeric ($number)){
$this->load->view('edit_record');
}else{
//LOAD TABLE (because ? without no ?id)
$this->load->view('options_view',$data);
}
}else{
//LOAD TABLE (becuase there is no ?id)
$this->load->view('options_view',$data);
}
} // end index
function create()
{
$data = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content')
);
$this->site_model->add_record($data);
$this->index();
}
function update(){
$id = $this->input->post('id'); // send id to model
$data['title'] = $this->input->post('title');
$data['content'] = $this->input->post('content');
$this->site_model->update_record($data, $id);
}
function delete()
{
$this->site_model->delete_row();
$this->index();
}
}
创建新视图(edit_record.php)
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled</title>
<style type="text/css" media="screen">
label{display:block;}
</style>
</head>
<body>
<h2>EDIT</h2>
<?php
$arr = $_SERVER['REQUEST_URI'];
$questionmark = explode('?', $arr);
$id = $questionmark[1];
$query = $this->db->get_where('assets', array('id' => $id));
if($query->result()){
echo form_open('site/update');
foreach ($query->result() as $row) {
?>
<p>
<label for="title">Title:</label>
<input type="text" name="title" id="title" value="<?php echo $row->title; ?>" />
</p>
<p>
<label for="content">Content:</label>
<input type="text" name="content" id="content" value="<?php echo $row->content; ?>" />
</p>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<p>
<input type="submit" value="UPDATE" />
</p>
<?php
}
echo form_close();
} else {
echo 'no record found with id <b>'. $id .'</b>';
}
?>
</body>
修改模型。要更新的目标ID资产
<?php
class Site_model extends CI_Model {
function get_records()
{
$query = $this->db->get('assets');
return $query->result();
// $query = $this->db->query('SELECT * FROM assets');
//echo $this->table->generate($query);
}
function add_record($data)
{
$this->db->insert('assets', $data);
return;
}
function update_record($data, $id)
{
$this->db->where('id', $id); // here is the id
$this->db->update('assets', $data);
redirect(site_url('site')); //redirect after done update process
}
function delete_row()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('assets');
}
}
修改您的options_view.php以在操作表中添加修改链接
<?php echo anchor('site?'. $row->Id, 'EDIT'); ?>