欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

thinkphp框架實(shí)現(xiàn)數(shù)據(jù)添加和顯示功能

 更新時(shí)間:2016年06月29日 14:25:15   作者:橙色時(shí)光  
這篇文章主要為大家詳細(xì)介紹了thinkphp框架實(shí)現(xiàn)數(shù)據(jù)添加和顯示功能的相關(guān)資料,需要的朋友可以參考下

最近的幾篇隨筆將都從thinkPHP框架的使用上著筆,好了,廢話不多說,下面是干貨。
 這篇文章將圍繞采用thinkPHP框架 向數(shù)據(jù)庫中添加數(shù)據(jù) 和 在網(wǎng)頁中顯示 這兩項(xiàng)功能進(jìn)行展示。
目的:在add頁添加數(shù)據(jù)后在lists頁進(jìn)行顯示(注意:由于thinkPHP框架已經(jīng)將list字段占用,因此在文件命名時(shí)不得使用形如“l(fā)ist.html”的命名方式)
預(yù)期頁面:    

 

下面就利用MVC架構(gòu)設(shè)計(jì)模式對(duì)其進(jìn)行實(shí)現(xiàn)
首先利用表單提交方式實(shí)現(xiàn)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">真實(shí)姓名:</span>
          <input type="text" class="form-control" placeholder="" id="input" name="realname">
         </div>
         <div class="input-group"> <span class="input-group-addon">手機(jī)號(hào)碼:</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">添加時(shí)間:</span>
          <input type="text" class="form-control" placeholder="2014-05-22" name="resgistertime">
         </div>
        <div class="input-group"> <span class="input-group-addon">設(shè)置密碼:</span>
          <input type="text" class="form-control" placeholder="123456" name="password">
         </div>
        <div class="input-group"> <span class="input-group-addon">確認(rèn)密碼:</span>
          <input type="text" class="form-control" placeholder="123456" name="repassword">
         </div>
         <div class="input-group">
          <button type="submit" class="btn btn-primary "> &nbsp;&nbsp;保<img src="__PUBLIC__/end/images/em.png" alt="" width="20" height="20">存&nbsp;&nbsp;</button>
         </div>
        </form> 

接下來是M模式部分,個(gè)人目前對(duì)這一部分的理解是    用來嚴(yán)重添加數(shù)據(jù)的合法性和給出錯(cuò)誤提示   。實(shí)現(xiàn)代碼如下:

<?php
namespace Admin\Model;
use Think\Model;

class AdminUsersModel extends Model {
  public $_validate = array (
    array("username", "require", "用戶名不能為空"),
    array("realname", "require", "真實(shí)姓名不能為空"),
    array("password", "require", "密碼不能為空"),
    array("repassword", "require", "確認(rèn)密碼不能為空"),
    array("telphone", "require", "電話不能為空"),
    array("email", "require", "郵箱不能為空"),
    array("resgistertime", "require", "注冊(cè)時(shí)間不能為空")
  );
} 

最后是純粹的邏輯C控制器部分啦,實(shí)現(xiàn)代碼如下:

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("添加失敗!");
  }
  
} 

以上就是整個(gè)實(shí)現(xiàn)過程了,希望對(duì)大家的學(xué)習(xí)有所幫助
友情鏈接thinkPHP參考手冊(cè):    http://document.thinkphp.cn/manual_3_2.html

原文作者:橙色時(shí)光

相關(guān)文章

最新評(píng)論