本章是快速入门篇,有基础可直接跳过:php
1.建立一个数据库名叫shopp的,而后穿件一张表html
#建立用户表 create table user( id int unsigned not null auto_increment primary key comment '用户编号', username varchar(50) not null default '' comment '用户名', email varchar(50) not null default '' comment '电子邮箱', password char(32) not null default '' comment '用户密码,md5加密', reg_time int unsigned not null default 0 comment '用户注册时间' )engine=MyISAM charset=utf8;
2.定义配置文件mysql
配置路径在:tp---Application---Common---Conf---config.php里面进行配置sql
咱们能够在手册上上直接找到数据库配置的参数,数据库
打开tp的手册,在附录下的--配置参考---数据库设置服务器
<?php return array( //'配置项'=>'配置值' //数据库配置 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址,由于数据库是在本地的因此写localhost 'DB_NAME' => 'shopp', // 数据库名 'DB_USER' => 'root', // 用户名 'DB_PWD' => '', // 密码由于的我密码是空,因此不写 'DB_PORT' => '3306', // 端口
'DB_PREFIX' => '', // 数据库表前缀若是么有建议不用谢这段,经测试报错
);
在项目中数据库配置文件通常都是值写一遍,不多回去更改的post
3.编写控制器测试
若是步知道控制器如何编写,能够借鉴他里面自带的那个进行修改,this
建立一个名字叫UserController.class.php的控制器加密
<?php //用户控制器 namespace Home\Controller; use Think\Controller; class UserController extends Controller { public function add(){ if(IS_POST){ //表单提交 return; } //载入表单 $this->display(); } }
4.准备视图文件
在view里面建立一个名为User的文件夹(一个控制器对应一个文件夹)而后在里面建立一个叫add.html的文件进行存放模版信息
add.html代码以下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <form action="" method="post"> <ul> <li><label>USER:<input type="text" name="username"></label></li> <li><label>PASS:<input type="password" name="password"></label></li> <li><label>邮箱:<input type="text" name="email"></label></li> <li><label><input type="submit" vaule="添加"></label></li> </ul> </form> </body> </html>
而后访问http://localhost/shopp/index.php/home/User/add便可看到刚才所建立的页面了
对Controller控制器进行完善
<?php //用户控制器 namespace Home\Controller; use Think\Controller; class UserController extends Controller { public function add(){ if(IS_POST){ //表单提交 $data['username'] = I('username'); $data['password'] = I('password'); $data['email'] = I('email'); $data['reg_time'] = time();//获取当前的时间戳 //使用模型弯沉插入操做 $mod = M('user'); if($mod->add($data)){ $this->success('添加用户成功',U('User/index')); }else{ $this ->error('添加用户失败'); } return; } //载入表单 $this->display(); } }
最后返回到add页面进行添加数据,而后在数据库里便可查看到。
咱们下一步能够一继续在控制器下写index方法了
<?php //用户控制器 namespace Home\Controller; use Think\Controller; class UserController extends Controller { public function index(){ //从数据库中取出数据 $user = M('User') -> select(); //dump($user);die; //分配数据到模版 $this->assign('user',$user); $this->display(); } public function add(){ if(IS_POST){ //表单提交 $data['username'] = I('username'); $data['password'] = I('password'); $data['email'] = I('email'); $data['reg_time'] = time();//获取当前的时间戳 //使用模型弯沉插入操做 $mod = M('user'); if($mod->add($data)){ $this->success('添加用户成功',U('User/index')); }else{ $this ->error('添加用户失败'); } return; } //载入表单 $this->display(); } }
这里完成了上面的了,那么下一步就是完成用户列表页了在Application\Home\View\User下建立一个index.html的文件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>用户列表</title> </head> <body> <table border="1" width="500"> <tr> <th>编号</th> <th>用户名</th> <th>邮箱</th> <th>操做</th> </tr> <volist name="user" id="vo"> <tr> <td>{$vo['id']}</td> <td>{$vo.username}</td> <td>{$vo.email}</td> <td><a href="">编辑</a>|<a href="">删除</a></td> </tr> </volist> </table> </body> </html>
下一步咱们就开始定义编辑和删除的入口
<td><a href="__CONTROLLER__/edit/id/{$vo['id']}">编辑</a> | <a href="__CONTROLLER__/del/id/{$vo['id']}">删除</a></td>
入口编辑完成那么咱们就开始写编辑和删除的功能了。
那么下一步就是在控制器UserController里面写个edit的方法了,代码以下:
public function edit(){ $id = I('id'); //获取id if(IS_POST){ //更新操做 $data['username'] = I('username'); $data['password'] = I('password'); $data['email'] = I('email'); $data['reg_time'] = time();//获取当前的时间戳 $data['id'] = $id; //使用模型弯沉插入操做 $mod = M('user'); if($mod->save($data)){ $this -> success('修改用户成功',U('User/index')); }else{ $this -> error('修改用户失败'); } return; } //载入编辑页面 $user = M('user') -> find($id); $this->assign('user',$user); $this-> display(); }
下一步在Application\Home\View\User下建立一个edit.html的文件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <form action="" method="post"> <ul> <li><label>USER:<input type="text" name="username" value="{$user.username}"></label></li> <li><label>PASS:<input type="password" name="password" value="{$user.password}"></label></li> <li><label>邮箱:<input type="text" name="email" value="{$user.email}"></label></li> <li><label><input type="submit" vaule="修改"></label></li> <input type="hidden" name="id" value="{$user.id}"> </ul> </form> </body> </html>
这个完成了最后一步就是删除了,这个实际上是最容易的,
在UserController里面写多个叫del的方法,以下代码所示:
<?php //用户控制器 namespace Home\Controller; use Think\Controller; class UserController extends Controller { public function index(){ //从数据库中取出数据 $user = M('User') -> select(); //dump($user);die; //分配数据到模版 $this->assign('user',$user); $this->display(); } public function add(){ if(IS_POST){ //表单提交 $data['username'] = I('username'); $data['password'] = I('password'); $data['email'] = I('email'); $data['reg_time'] = time();//获取当前的时间戳 //使用模型弯沉插入操做 $mod = M('user'); if($mod->add($data)){ $this->success('添加用户成功',U('User/index')); }else{ $this ->error('添加用户失败'); } return; } //载入表单 $this->display(); } public function edit(){ $id = I('id'); //获取id if(IS_POST){ //更新操做 $data['username'] = I('username'); $data['password'] = I('password'); $data['email'] = I('email'); $data['reg_time'] = time();//获取当前的时间戳 $data['id'] = $id; //使用模型弯沉插入操做 $mod = M('user'); if($mod->save($data)){ $this -> success('修改用户成功',U('User/index')); }else{ $this -> error('修改用户失败'); } return; } //载入编辑页面 $user = M('user') -> find($id); $this->assign('user',$user); $this-> display(); } public function del(){ $id = I('id'); if(M('user') -> delete($id)){ $this -> success('删除用户成功',U('User/index')); }else{ $this -> error('删除用户失败'); } } }
便可完成删除功能