thinkphp框架实现数据添加和显示功能

yipeiwu_com6年前PHP代码库

最近的几篇随笔将都从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 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控制器部分啦,实现代码如下:

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

原文作者:橙色时光

相关文章

浅谈PHP 闭包特性在实际应用中的问题

呃,其实大部分情况下是可以的,而有些方面还是令人非常的困扰,下面慢慢道来。 很多语言的都提供了非常优雅和漂亮的操作数组的方法。在下面的例子中,会使用 PHP5.3 以及其他语言提供的闭包...

php面向对象编程self和static的区别

在php的面向对象编程中,总会遇到 class test{ public static function test(){ self::func(); static::fu...

php中将一个对象保存到Session中的方法

本文实例讲述了php中将一个对象保存到Session中的方法。分享给大家供大家参考。具体如下: 要保存对象到session其实很简单,我们可以使用session_register()函数...

PHP SPL 被遗落的宝石【SPL应用浅析】

PHP SPL 被遗落的宝石【SPL应用浅析】

本文实例讲述了PHP SPL应用方法。分享给大家供大家参考,具体如下: Rafael Dohms 上面的篇文章 让我为之惊艳,忍不住就翻译了下来,同时补充了部分内容。 SPL,PHP 标...

php设计模式 Proxy (代理模式)

代理,指的就是一个角色代表另一个角色采取行动,就象生活中,一个红酒厂商,是不会直接把红酒零售客户的,都是通过代理来完成他的销售业务。而客户,也不用为了喝红酒而到处找工厂,他只要找到厂商在...