ThinkPHP - CURD增删改查 - 实例

目录结构:

 

UserAction.class.php:

<?php
//编写控制器类
class UserAction extends Action {
	/**
	 * 显示主页面,入口文件
	 * @return  无返回值
	 */
	public function index(){
		//创建实体模型
		$user = M('User');
		//查询所有数据
		$dataArr = $user->select();
		//向前台模板注入数据
		$this->assign('data', $dataArr);
		//显示模板文件
		$this->display();
	}

	/**
	 * 显示修改数据页面
	 * @return  无返回值
	 */
	public function modify(){
		//创建实体模型
		$user = M('User');
		//接受传递的id参数
		$id = $_GET['id'];
		//获取数据
		$dataArr = $user->find($id);
		//注入到前台页面
		$this->assign('data', $dataArr);
		//显示模板文件
		$this->display();
	}

	/**
	 * 显示添加用户页面
	 * @return  无返回值
	 */
	public function add(){
		//显示添加页面
		$this->display();
	}

	/**
	 * 删除数据
	 * @return  无返回值
	 */
	public function del(){
		//创建实体模型
		$user = M('User');
		//接受传递的get参数
		$id = $_GET['id'];
		//根据id删除数据
		$result = $user->delete($id);
		//判断结果
		if ( $result > 0 ) {
			$this->success('删除成功!');
		} else {
			$this->error('删除失败!');
		}
	}

	/**
	 * 修改数据
	 * @return  无返回值
	 */
	public function update(){
		//创建实体模型
		$user = M('User');
		//接受传递来的参数
		$id = $_POST['id'];
		$dataArr['username'] = $_POST['username'];
		$dataArr['sex'] = $_POST['sex'];
		//修改
		$result = $user->where('id=' . $id)->save($dataArr);
		//判断
		if ( $result > 0 ) {
			$this->success('修改成功!', 'index');
		} else {
			$this->error('修改失败!');
		}
	}

	/**
	 * 添加数据
	 * @return  无返回值
	 */
	public function create(){
		//创建实体模型
		$user = M('User');
		//接收要添加的数据
		$dataArr['username'] = $_POST['username'];
		$dataArr['sex'] = $_POST['sex'];
		//添加
		$result = $user->add($dataArr);
		//判断
		if ( $result > 0 ) {
			$this->success('添加成功!', 'index');
		} else {
			$this->error('添加失败!');
		}
	}
}

 

删除效果:

  • 直接点击页面即可删除。删除的链接为 del方法,传递id参数。

 

 

add.html:

 

<!DOCTYPE html>
<html>
<head>
	<title>添加页面</title>
</head>
<body>
	<form action="__URL__/create" method="POST">
		姓名:<input type="text" name="username" /><br/>
		性别:
			男<input type="radio" name="sex" value="1"  />
			女<input type="radio" name="sex" value="0"  /><br/>
		<input type="submit" name="submit" value="添加新用户" />
	</form>
</body>
</html>

 

modify.html:

 

<!DOCTYPE html>
<html>
<head>
	<title>修改页面</title>
</head>
<body>
	<form action="__URL__/update" method="POST">
		<input type="hidden" name="id" value="<{$data.id}>" />
		姓名:<input type="text" name="username" value="<{$data.username}>" /><br/>
		性别:
			男<input type="radio" name="sex" value="1" />
			女<input type="radio" name="sex" value="0" /><br/>
		<input type="submit" name="submit" value="提交修改" />
	</form>
</body>

	<script type="text/javascript">
		//判断性别
		if (<{$data.sex}> == 0) {
			//男
			document.getElementsByName("sex")[1].checked="checked";
		} else {
			//女
			document.getElementsByName("sex")[0].checked="checked";
		}
	</script>
</html>

 

index.html:

 

<!DOCTYPE html>
<html>
<head>
	<title>用户列表页面</title>
	<script type="text/javascript">
		function jumpPage(){
			window.location = '__URL__/add';
		}
	</script>
</head>
<body>
	<table border='1' width="500">
		<tr>
			<th>编号</th>
			<th>姓名</th>
			<th>性别</th>
			<th>操作</th>
		</tr>
		<volist name='data' id='vo'>
			<tr>
				<td>
					<{$vo.id}>
				</td>
				<td>
					<{$vo.username}>
				</td>
				<td>
					<{$vo.sex}>
				</td>
				<td>
					<a href="__URL__/del/id/<{$vo.id}>">删除</a>
					|
					<a href="__URL__/modify/id/<{$vo.id}>">修改</a>
				</td>
			</tr>
		</volist>
	</table>
	<button onclick="jumpPage()">添加用户</button>
</body>
</html>

 

posted on 2016-01-30 16:40  ultrastrong  阅读(260)  评论(0编辑  收藏  举报