1.CURD 控制器
?>
namespace Home\Controller;
use Think\Controller;
class CurdController extends Controller{
public function index(){
$db_student = D(‘Student‘);
$data_student = $db_student->relation(true)->select();
// dump($data_student);
$this->assign(‘data‘,$data_student);
$this->display();
}
// public function extends
}
2.cued控制器html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改实例</title>
</head>
<body>
<table border="1" width="500">
<tr>
<td>
序号
</td>
<td>
姓名
</td>
<td>
性别
</td>
<td>
创建时间
</td>
<td>
班级编号
</td>
<td>
操作
</td>
</tr>
<volist name="data" id="vo">
<tr>
<td>
{$vo[‘id‘]}
<td>
{$vo.name}
</td>
<td>
<if condition="$vo[‘sex‘] eq 1">男<else/>女</if>
</td>
<td>
{$vo.create_time}
</td>
<td>
{$vo[‘class‘].class_name}
</td>
<td>
<a href="{:U(‘Edit/Edit‘,array(‘id‘=>$vo[‘id‘]))}">编辑</a>
</td>
</volist>
</table>
</body>
</html>
3.edit控制器
<?php
/**
* Created by 郭鹏.
* User: msi
* Date: 2017/12/15
* Time: 12:41
*/
namespace Home\Controller;
use Think\Controller;
class EditController extends Controller{
public function Edit(){
if(IS_GET) {
// dump(I(‘get.‘));
$id = I(‘get.id‘);
$db_class = M(‘Class‘);
$db_student = M(‘Student‘);
$data_class = $db_class->select();
$data_student = $db_student->where(‘id = ‘ . $id)->find();
// dump($data_student);
$this->assign(‘class‘, $data_class);
$this->assign(‘student‘, $data_student);
$this->display(‘index‘);
}
}
public function editpost(){
if (IS_POST){
$data = I(‘post.‘);
$db_student = M(‘Student‘);
dump($data);
// $data[‘name‘] = $_POST[‘name‘];
// $data[‘sex‘] = $_POST[‘sex‘];
// $data[‘class_id‘] = $_POST[‘class_id‘];
$res = $db_student->save($data); // 根据条件更新记录
if($res) {
$this->success(‘恭喜你成功‘);
}else{
$this->success(‘修改失败‘);
}
}
}
}
4.获取信息页面并提交修改信息
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="{:U(‘Edit/editpost‘)}" method="post">
<table border="1" width="50%">
<tr>
<td>
序号
</td>
<td>
姓名
</td>
<td>
性别
</td>
<td>
创建时间
</td>
<td>
班级名称
</td>
</tr>
<tr>
<td>
{$student[‘id‘]}
<td>
<input type="text" name="name" value="{$student.name}">
</td>
<td>
<if condition="$student[‘sex‘] eq 1">
<input type="radio" name="sex" value="1" id="man" checked><label>男</label>
<input type="radio" name="sex" value="0" id="woman"><label>女</label>
<else/>
<input type="radio" name="sex" value="1" id="man"><label>男</label>
<input type="radio" name="sex" value="0" id="woman" checked><label>女</label>
</if>
</td>
<td>
<input type="text" name="create_time" value="{$student.create_time}">
</td>
<td>
<select name="class_id">
<volist name="class" id="vo">
<if condition="$student[‘class_id‘] eq $vo[‘class_id‘]">
<option value="{$vo.id}" selected>{$vo.class_name}</option>
<else/>
<option value="{$vo.id}">{$vo.class_name}</option>
</if>
</volist>
</select>
</td>
<td>
<input type="hidden" name="id" value="{$student[‘id‘]}">
<input type="submit" value="提交">
</td>
</table>
</form>
</body>
</html>