|
======================================
面向?qū)ο蟮?a href=/itjie/phpjishu/ target=_blank class=infotextkey>php開發(fā)模式(待完善中。。。)
======================================
一、環(huán)境
服務(wù)器:Linux (Apache 2.x, MySQL4.1.x, php4, Perl, SHELL, CVS, Sambar)
客戶端:Windows (Ie6, UltraEdit, 其它輔助工具)
測試機(jī):windows98/2K/xp/Linux (Ie5, Ie6, mozilla, firefox)
二、網(wǎng)頁、程序、數(shù)據(jù)庫的三層
所謂的網(wǎng)頁并不是一般的靜態(tài)網(wǎng)頁,這里的網(wǎng)頁是根據(jù)項(xiàng)目分析的具體情況進(jìn)行拆分
后用html做的模板;這里的數(shù)據(jù)庫包括數(shù)據(jù)庫和與其它部分的接口程序,通常程序和數(shù)據(jù)庫
程序可能會混合在一個文件里,但應(yīng)該用函數(shù)的方式把它們盡量分開,其它程序如果要用數(shù)
據(jù)庫直接調(diào)用這些函數(shù)即可,不能直接接觸SQL語句。
三、項(xiàng)目分析--數(shù)據(jù)分析
一個項(xiàng)目在得到需求分析后,實(shí)際開發(fā)前第一步應(yīng)該做的就是數(shù)據(jù)分析。數(shù)據(jù)分析就是
把項(xiàng)目過程中要用到的各式各樣的數(shù)據(jù)堆在一塊,根據(jù)它們的特點(diǎn)進(jìn)行分類再分別組織,當(dāng)
然它們之間還可能存在著各式各樣的關(guān)聯(lián)關(guān)系。做好這一步就使項(xiàng)目分析工作得到了一個良
好的開端,為下面的項(xiàng)目結(jié)構(gòu)分析及數(shù)據(jù)處理的流程分析也提供了極大的方便。
四、項(xiàng)目分析--數(shù)據(jù)抽象
由數(shù)據(jù)分析后我們的腦子中應(yīng)該能出現(xiàn)一些大致的數(shù)據(jù)模型及一些基本數(shù)據(jù)小模型組合
而成的大模型,一般情況下,我們把一些需要變化的數(shù)據(jù)建立數(shù)據(jù)庫來進(jìn)行維護(hù),不需要變
化的數(shù)據(jù)做成一些常量,并針對這些數(shù)據(jù)類型抽象出相關(guān)的類,并建立進(jìn)行數(shù)據(jù)庫操作的相
關(guān)接口(函數(shù)形式,即方法),數(shù)據(jù)與數(shù)據(jù)的相關(guān)聯(lián)的操作也可以抽象出一些基本的方法,
我們只需要在程序設(shè)計(jì)中進(jìn)行調(diào)用即可。
五、項(xiàng)目分析--界面分析
我們分析好了數(shù)據(jù),目的是組合出一個或者幾個產(chǎn)品,而既然要做產(chǎn)品就要給別人看。
所以我們還要進(jìn)行界面設(shè)計(jì),當(dāng)各種界面盡量考慮全面后,就將設(shè)計(jì)的界面制作成模板,并
寫出相應(yīng)的處理接口程序(所以,在程序眼里,界面也是一種數(shù)據(jù)),在寫程序時進(jìn)行使用。
六、項(xiàng)目分析--流程設(shè)計(jì)
網(wǎng)站式程序非常簡單,按照流程調(diào)用我們設(shè)計(jì)好的各種數(shù)據(jù)即可。
七、案例分析
用戶系統(tǒng),現(xiàn)在我們分析一個最簡單的例子,一個用戶系統(tǒng)。
1. 數(shù)據(jù)分析,我們分析一個最簡單的用戶系統(tǒng),所以這里只有兩個數(shù)據(jù),那就是用戶名
和密碼,繼續(xù)分析還會想到我們應(yīng)該給每條記錄加一個編號(id),現(xiàn)在有了三個數(shù)據(jù),實(shí)在沒
有再可以添加的了。
2. 數(shù)據(jù)抽象,只有三個數(shù)據(jù)的數(shù)據(jù)模型,想到它可能出現(xiàn)的操作方法,我們做如下安排,
數(shù)據(jù)庫接口(savetodb(), getfromdb(), delete()),分別為數(shù)據(jù)入庫及出庫還有刪除;更改密
碼(password())。另外考慮到對用戶系統(tǒng)的管理及查看,所以會有一個集合類型的數(shù)據(jù)(list)。
3. 界面分析,登陸,驗(yàn)證成功,驗(yàn)證出錯,修改密碼,修改密碼成功,修改密碼出錯,用
戶注冊,注冊成功,注冊出錯;管理--用戶列表,管理--用戶信息查看,管理--修改用戶
密碼,管理--刪除用戶。
4. 示例代碼
php 代碼:
復(fù)制代碼 代碼如下:
<?php
include_once "include.php";
/*
** 用途:用戶系統(tǒng)數(shù)據(jù)抽象
** 作者:岳信明
** 時間:2005-8-30 10:05
*/
class User {
var $id = 0;
var $Name = "";
var $Password = "";
var $db = "";
var $tpl = "";
/*
** 函數(shù)功能:構(gòu)造函數(shù),指定類使用的數(shù)據(jù)庫連接
** 參數(shù)說明:$tpl,顯示模板處事句柄;$userdb,數(shù)據(jù)庫連接
** 返 回 值:無
** 作 者:岳信明
** 創(chuàng)建時間:2005-8-30 10:37
*/
function User($vtpl = "", $userdb = "") {
if ($vtpl == "") {
global $tpl; // 外部定義數(shù)據(jù)庫連接
$this->tpl =& $tpl;
} else {
$this->tpl = $vtpl;
}
if ($userdb == "") {
global $db; // 外部定義數(shù)據(jù)庫連接
$this->db =& $db;
} else {
$this->db = $userdb;
}
}
/*
** 函數(shù)功能:將數(shù)據(jù)存入數(shù)據(jù)庫
** 參數(shù)說明:無參數(shù)
** 返 回 值:true/false,成功/失敗
** 作 者:岳信明
** 創(chuàng)建時間:2005-8-30 10:24
*/
function savetodb() {
if ($this->Name == "") {
return false;
}
if ($this->id) {
$strSQL = sprintf("UPDATE user SET Name='%s', Password='%s' "
. "WHERE id='%s'",
$this->Name,
$this->Password,
$this->id
);
} else {
$strSQL = sprintf("INSERT user (Name, Password) "
. "VALUES ('%s', '%s')",
$this->Name,
$this->Password
);
}
if ($this->db->query($strSQL)) {
return true;
} else {
return false;
}
}
/*
** 函數(shù)功能:從數(shù)據(jù)庫中獲取記錄
** 參數(shù)說明:$id,記錄編號
** 返 回 值:true/false,成功/失敗
** 作 者:岳信明
** 創(chuàng)建時間:2005-8-30 10:32
*/
function getfromdb($id = 0) {
if ($id) {
$strSQL = sprintf("SELECT * FROM user WHERE id='%s'", $id);
} else if ($this->id) {
$strSQL = sprintf("SELECT * FROM user WHERE id='%s'",
$this->id
);
} else if ($this->Name != "") {
$strSQL = sprintf("SELECT * FROM user WHERE Name='%s'",
$this->Name
);
} else {
return false;
}
$this->db->query($strSQL);
if ($this->db->next_record()) {
$this->id = $this->db->f("id");
$this->Name = $this->db->f("Name");
$this->Password = $this->db->f("Password");
return true;
} else {
return false;
}
}
/*
** 函數(shù)功能:從數(shù)據(jù)庫中刪除記錄
** 參數(shù)說明:$id,記錄編號
** 返 回 值:true/false,成功/失敗
** 作 者:岳信明
** 創(chuàng)建時間:2005-8-30 10:47
*/
function delete($id = 0) {
if (is_array($id)) { // 同時刪除多條記錄
foreach($id as $i) {
$strSQL = sprintf("DELETE FROM user WHERE id='%s'", $i);
$this->db->query($strSQL);
}
return true;
} else if ($id) {
$strSQL = sprintf("DELETE FROM user WHERE id='%s'", $id);
} else if ($this->id) {
$strSQL = sprintf("DELETE FROM user WHERE id='%s'", $this->id);
} else {
return false;
}
$this->db->query($strSQL);
return true;
}
/*
** 函數(shù)功能:顯示登陸界面
** 參數(shù)說明:$placeholder,顯示位置
** 返 回 值:無
** 作 者:岳信明
** 創(chuàng)建時間:2005-8-30 11:00
*/
function showLogin($placeholder) {
$this->tpl->addBlockfile($placeholder, "user_showLogin",
"tpl.user_showLogin.html"
);
$this->tpl->setCurrentBlock("user_showLogin");
$this->tpl->setVariable(array("user_Logintitle" => "用戶登陸",
"strUsername" => "用戶名",
"strPassword" => "密 碼"
)
);
$this->tpl->parseCurrentBlock("user_showLogin");
}
/*
** 函數(shù)功能:處理登陸信息
** 參數(shù)說明:$placeholder,顯示位置
** 返 回 值:true/false,成功/失敗
** 作 者:岳信明
** 創(chuàng)建時間:2005-8-30 11:12
*/
function getLogin($placeholder = "") {
if (isset($_POST["login"])) {
if ($_POST["username"] == "") {
if ($placeholder != "") {
$this->tpl->setVarable($placeholder, "用戶名不能為空!");
}
return false;
}
$this->Name = $_POST["username"];
$this->getfromdb();
if ($this->Password() == $_POST["password"]) {
return true;
}
} else {
if ($placeholder != "") {
$this->tpl->setVarable($placeholder, "登陸失敗!");
}
return false;
}
}
/*
** 函數(shù)功能:顯示注冊界面
** 參數(shù)說明:$placeholder,顯示位置
** 返 回 值:無
** 作 者:岳信明
** 創(chuàng)建時間:2005-8-30 13:33
*/
function showRegister($placeholder) {
$this->tpl->addBlockfile($placeholder, "user_showRegister",
"tpl.user_showRegister.html"
);
$this->setCurrentBlock("user_shoRegister");
// 在這里完成處理模板的代碼
...
$this->parseCurrentBlock("user_shoRegister");
}
/*
** 函數(shù)功能:處理注冊信息
** 參數(shù)說明:$placeholder,顯示位置
** 返 回 值:true/false,注冊成功/注冊失敗
** 作 者:岳信明
** 創(chuàng)建時間:2005-8-30 15:49
*/
function getRegister($placeholder = "") {
if (isset($_POST["register")) {
if ($_POST["username"] == "") { // 用戶名合法性檢查,可改成其它檢查方式
if ($placeholder != "") { // 錯誤提示
$this->tpl->setVariable($placeholder, "用戶名不合法!");
}
return false;
}
if ($_POST["password"] != $_POST["repassword"]) { // 密碼合法性檢查
if ($placeholder != "") { // 錯誤提示
$this->tpl->setVariable($placeholder, "兩次輸入密碼不一致!");
}
return false;
}
$strSQL = sprintf("SELECT COUNT(*) FROM user "
. "WHERE Name='%s'",
$this->Name
);
$this->db->query($strSQL);
$this->db->next_record();
if ($this->db->f("COUNT(*)") > 0) {
return false;
} else {
$strSQL = sprintf("INSERT INTO user (Name, Password) "
. "VALUES('%s', '%s')",
$this->Name,
$this->Password
);
$this->db->query($strSQL);
return true;
}
} else {
return false;
}
}
} // 類User定義結(jié)束
/*
** 用途:用戶系統(tǒng)數(shù)據(jù)列表抽象
** 作者:岳信明
** 時間:2005-8-30 17:21
*/
class UserList {
var $page = 0;
var $pages = 0;
var $pagesize = 9;
var $recordsum = 0;
var $Users = array();
var $c;
var $db = "";
var $tpl = "";
/*
** 函數(shù)功能:構(gòu)造函數(shù),新建一個類時對一些變量進(jìn)行初始化
** 參數(shù)說明:無參數(shù)
** 返 回 值:無
** 作 者:岳信明
** 創(chuàng)建時間:2005-8-30 15:49
*/
function UserList($page = 1, $pagesize = 10,
$c, $vtpl = "", $vdb = "") {
$this->page = $page;
$this->pagesize = $pagesize;
$this->condition = $condition;
if ($vdb != "") {
$this->db = $vdb;
} else {
global $db;
$this->db = $db;
}
if ($vtpl != "") {
$this->tpl = $vtpl;
} else {
$this->tpl = $tpl;
}
$strSQL = sprintf("SELECT COUNT(*) FROM user WHERE '%s'",
$this->condition
);
$this->db->query($strSQL);
$this->db->next_record();
$this->recordsum = $this->db->f("COUNT(*)");
$this->pages = ceil($this->recordsum / $this->pagesize);
$strSQL = sprintf("SELECT * FROM user WHERE '%s' LIMIT '%s', '%s'",
$this->condition,
$this->page * $this->pagesize,
$this->pagesize + 1
);
$this->db->query($strSQL);
for ($i = 0; $this->db->next_record(); $i ++) {
$this->Users[$i] = new User($this->tpl, $this->db);
$this->Users[$i]->id = $this->db->f("id");
$this->Users[$i]->Name = $this->db->f("Name");
$this->Users[$i]->Password = $this->db->f("Password");
}
}
/*
** 函數(shù)功能:顯示列表
** 參數(shù)說明:$placeholder,顯示位置
** 返 回 值:無
** 作 者:岳信明
** 創(chuàng)建時間:2005-8-31 9:16
*/
function showUserList($placeholder) {
$this->tpl->addBlockfile($placeholder, "showUserList", "tpl.showUserList.html");
$this->tpl->setCurrentBlock("showUserList");
//在這里添加相應(yīng)的處理代碼
$this->tpl->setVariable("strTitle", "用戶列表");
$strTitles = array("用戶名", "操作");
$RecordOperations = array("重設(shè)密碼" => "operate=passwd&id=",
"刪除" => "operate=delete&id="
);
// 顯示表頭
foreach ($strTitles as $title) {
$this->tpl->setCurrentBlock("showRecordsTitle");
$this->tpl->setVariable("strHead", $title);
$this->tpl->parseCurrentBlock("showRecordsTitle");
}
// 顯示記錄及相關(guān)操作
if (is_array($this->Users)) { // 有記錄
foreach ($this->Users as $user) {
$this->tpl->setCurrentBlock("showRecords");
$this->tpl->setCurrentBlock("showCell");
$this->tpl->setVariable("strCell", $user);
$this->tpl->parseCurrentBlock("showCell");
$this->tpl->setCurrentBlock("showCell");
foreach ($RecordOperations as $operation => $linkOperation) {
$this->tpl->setCurrentBlock("showOperations");
$this->tpl->setVariable("strOperation", $operation);
$this->tpl->setVariable("strLink", $_SERVER["REQUEST_URI"] . $linkOperation . $user->id);
$this->tpl->parseCurrentBlock("showOperations");
}
$this->tpl->parseCurrentBlock("showCell");
$this->tpl->parseCurrentBlock("showRecords");
}
} else { // 無記錄
$this->tpl->setCurrentBlock("showRecords");
$this->tpl->setCurrentBlock("showCell");
$this->tpl->setVariable("strCell", "無記錄");
$this->tpl->parseCurrentBlock("showCell");
$this->tpl->setCurrentBlock("showCell");
$this->tpl->setVariable("strCell", " ");
$this->tpl->parseCurrentBlock("showCell");
$this->tpl->parseCurrentBlock("showRecords");
}
$this->tpl->setCurrentBlock("showPageInfo");
$this->tpl->setVariable(array("intColspan" => "2",
"intRecordSum" => $this->recordsum,
"intPage" => $this->page,
"intPages" => $this->pages
)
);
$this->tpl->parseCurrentBlock("showPageInfo");
$this->tpl->parseCurrentBlock("showUserList");
}
}
?> <!-- php buffer end -->
HTML 代碼:
[Ctrl+A 全選 注:如需引入外部Js需刷新才能執(zhí)行]
php技術(shù):不錯的一篇面向?qū)ο蟮腜HP開發(fā)模式(簡寫版),轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。