《PHP實(shí)例:thinkphp框架實(shí)現(xiàn)數(shù)據(jù)添加和顯示功能》要點(diǎn):
本文介紹了PHP實(shí)例:thinkphp框架實(shí)現(xiàn)數(shù)據(jù)添加和顯示功能,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
相關(guān)主題:thinkphp教程
PHP應(yīng)用最近的幾篇隨筆將都從thinkPHP框架的使用上著筆,好了,廢話不多說(shuō),下面是干貨.
?這篇文章將圍繞采用thinkPHP框架 向數(shù)據(jù)庫(kù)中添加數(shù)據(jù) 和 在網(wǎng)頁(yè)中顯示 這兩項(xiàng)功能進(jìn)行展示.
目的:在add頁(yè)添加數(shù)據(jù)后在lists頁(yè)進(jìn)行顯示(注意:由于thinkPHP框架已經(jīng)將list字段占用,因此在文件命名時(shí)不得使用形如“l(fā)ist.html”的命名方式)
預(yù)期頁(yè)面:????
PHP應(yīng)用?
PHP應(yīng)用下面就利用MVC架構(gòu)設(shè)計(jì)模式對(duì)其進(jìn)行實(shí)現(xiàn)
首先利用表單提交方式實(shí)現(xiàn)V視圖部分,代碼如下:
PHP應(yīng)用
<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 "> ??保<img src="__PUBLIC__/end/images/em.png" alt="" width="20" height="20">存??</button>
</div>
</form>
PHP應(yīng)用接下來(lái)是M模式部分,個(gè)人目前對(duì)這一部分的理解是??? 用來(lái)嚴(yán)重添加數(shù)據(jù)的合法性和給出錯(cuò)誤提示?? .實(shí)現(xiàn)代碼如下:
PHP應(yīng)用
<?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í)間不能為空")
);
}
PHP應(yīng)用最后是純粹的邏輯C控制器部分啦,實(shí)現(xiàn)代碼如下:
PHP應(yīng)用
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("添加失敗!");
}
}
PHP應(yīng)用以上就是整個(gè)實(shí)現(xiàn)過(guò)程了,希望對(duì)大家的學(xué)習(xí)有所幫助
友情鏈接thinkPHP參考手冊(cè):??? http://document.thinkphp.cn/manual_3_2.html
PHP應(yīng)用原文作者:橙色時(shí)光
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/6080.html