最近的几篇随笔将都从thinkPHP框架的使用上着笔,好了,废话不多说,下面是干货。
这篇文章将围绕采用thinkPHP框架 向数据库中添加数据 和 在网页中显示 这两项功能进行展示。
目的:在add页添加数据后在lists页进行显示(注意:由于thinkPHP框架已经将list字段占用,因此在文件命名时不得使用形如“list.html”的命名方式)
预期页面:
下面就利用MVC架构设计模式对其进行实现
首先利用表单提交方式实现V视图部分,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
< form role = "form" method = "post" action = "__MODULE__/Admin/User/doAdd" >
< div class = "input-group" > < span class = "input-group-addon" >用< img src = "__PUBLIC__/end/images/em.png" alt = "" width = "6" height = "20" >户< img src = "__PUBLIC__/end/images/em.png" alt = "" width = "6" height = "20" >名:</ span >
< input type = "text" class = "form-control" placeholder = "" name = "username" >
</ div >
< div class = "input-group " > < span class = "input-group-addon" for = "inputWarning1" >真实姓名:</ span >
< input type = "text" class = "form-control" placeholder = "" id = "input" name = "realname" >
</ div >
< div class = "input-group" > < span class = "input-group-addon" >手机号码:</ span >
< input type = "text" class = "form-control" placeholder = "" name = "telphone" >
</ div >
< div class = "input-group" > < span class = "input-group-addon" >电子邮箱:</ span >
< input type = "text" class = "form-control" placeholder = "" name = "email" >
</ div >
< div class = "input-group" > < span class = "input-group-addon" >添加时间:</ span >
< input type = "text" class = "form-control" placeholder = "2014-05-22" name = "resgistertime" >
</ div >
< div class = "input-group" > < span class = "input-group-addon" >设置密码:</ span >
< input type = "text" class = "form-control" placeholder = "123456" name = "password" >
</ div >
< div class = "input-group" > < span class = "input-group-addon" >确认密码:</ span >
< input type = "text" class = "form-control" placeholder = "123456" name = "repassword" >
</ div >
< div class = "input-group" >
< button type = "submit" class = "btn btn-primary " > 保< img src = "__PUBLIC__/end/images/em.png" alt = "" width = "20" height = "20" >存 </ button >
</ div >
</ form >
|
接下来是M模式部分,个人目前对这一部分的理解是 用来严重添加数据的合法性和给出错误提示 。实现代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
namespace Admin\Model;
use Think\Model;
class AdminUsersModel extends Model {
public $_validate = array (
array ( "username" , "require" , "用户名不能为空" ),
array ( "realname" , "require" , "真实姓名不能为空" ),
array ( "password" , "require" , "密码不能为空" ),
array ( "repassword" , "require" , "确认密码不能为空" ),
array ( "telphone" , "require" , "电话不能为空" ),
array ( "email" , "require" , "邮箱不能为空" ),
array ( "resgistertime" , "require" , "注册时间不能为空" )
);
}
|
最后是纯粹的逻辑C控制器部分啦,实现代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public function add(){
$this ->display();
}
public function doAdd(){
if (!IS_POST) {
exit ( "bad request!" );
}
$adminUsersModel = D( "AdminUsers" );
if (! $adminUsersModel ->create()) {
$this ->error( $adminUsersModel ->getError());
}
if ( $adminUsersModel ->add()) {
$this ->success( "添加成功!" ,U( "Admin/User/lists" ));
}
else {
$this ->error( "添加失败!" );
}
}
|
以上就是整个实现过程了,希望对大家的学习有所帮助
友情链接thinkPHP参考手册: http://document.thinkphp.cn/manual_3_2.html
原文作者:橙色时光