最近的几篇随笔将都从thinkphp框架的使用上着笔,好了,废话不多说,下面是干货。
这篇文章将围绕采用thinkphp框架 向数据库中添加数据 和 在网页中显示 这两项功能进行展示。
目的:在add页添加数据后在lists页进行显示(注意:由于thinkphp框架已经将list字段占用,因此在文件命名时不得使用形如“list.html”的命名方式)
预期页面:
下面就利用mvc架构设计模式对其进行实现
首先利用表单提交方式实现v视图部分,代码如下:
<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模式部分,个人目前对这一部分的理解是 用来严重添加数据的合法性和给出错误提示 。实现代码如下:
<?php
namespace adminmodel;
use thinkmodel;
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控制器部分啦,实现代码如下:
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
原文作者:橙色时光
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!