本章是快速入門篇,有基礎可直接跳過: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('刪除用戶失敗'); } } }
便可完成刪除功能