[B]9、类库代码[/B][/COLOR]
database.class.php
<?php
class database {
var $hostname = "localhost";
var $username = "root";
var $password = "pass";
var $basename = "web";
var $query = "";
var $result = NULL;
var $db = NULL;
/*
//初始化
function database() {
$this->connect();
$this->selectDatabase();
}
*/
//连接数据库
function connect() {
$this->db = @mysql_connect($this->hostname, $this->username, $this->password);
if(FALSE == $this->db) {
echo "无法正常连接数据库。";
exit;
}
}
//选择数据库
function selectDatabase() {
if(FALSE == mysql_select_db($this->basename)) {
echo "无法正常打开选定的数据库。";
return FALSE;
exit;
}
return TRUE;
}
//设置语句
function setQuery($myQuery) {
if($myQuery == "") {
echo "语句不能为空。";
return FALSE;
} else {
$this->query = $myQuery;
return TRUE;
}
}
//更新数据库
function store() {
$this->result = mysql_query($this->query, $this->db);
$this->query = "";
}
//关闭数据库
function closeDatabase() {
mysql_close($this->db);
}
}
/*测试
$test = new database;
$test->setQuery("select * from class");
$test->store();
$row = mysql_fetch_array($test->result);
echo "<pre>";
print_r($row);
echo "</pre>";
$test->closeDatabase();
*/
?>
kind.class.ph
<?php
include_once("database.class.php");
include_once("news.class.php");
class kind extends database {
var $idKind = NULL;
var $nameKind = NULL;
var $tableName = "web_kind";
//初始化 没办法,只有PHP5才支持重载-_-#
function kind() {
$this->connect();
$this->selectDatabase();
switch(func_num_args()) {
case 1 : $this->setKindId(func_get_arg(0));
$this->setKindName($this->getKindName($this->idKind));
break;
case 2 : $this->setKindId(func_get_arg(0));
$this->setKindName(func_get_arg(1));
break;
default :
exit;
}
}
//是否已存在,返回数目
function findKind() {
$this->setQuery("select count('kind_id') from $this->tableName");
$this->store();
$numKind = mysql_fetch_array($this->result);
return $numKind[0];
mysql_free_result($this->result);
}
//添加种类
function insertKind() {
$this->setQuery("insert into $this->tableName set kind_name = '$this->nameKind' ");
$this->store();
return $this->result;
}
//修改种类
function updateKind() {
$this->setQuery("update $this->tableName set kind_name = '$this->nameKind' where kind_id = '$this->idKind'");
$this->store();
return $this->result;
}
//删除种类
function delKind() {
$this->setQuery("delete from $this->tableName where kind_id = '$this->idKind'");
$this->store();
return $this->result;
}
//显示种类列表,可以自行定义样式
function showKind() {
$this->setQuery("select * from $this->tableName order by kind_id");
$this->store();
echo "<ul>";
while($row = mysql_fetch_array($this->result)) {
echo '<li><a href="'.$row["kind_id"].'">'.$row["kind_name"].'</a></li>';
}
echo "</ul>";
mysql_free_result($this->result);
}
//返回Kind类的id
function returnKindId() {
return $this->idKind;
}
//返回Kind类的name
function returnKindName() {
return $this->nameKind;
}
//设置Kind类的name,检查长度等操作
function setKindName($name) {
if(strlen($name) >= 12) {
//Call errorDialog
exit;
} else {
$this->nameKind = $name;
return TRUE;
}
}
//设置Kind类的id,防止非正整数等
function setKindId($id) {
if($id < 0) {
//Call errorDialog
exit;
} else {
$this->idKind = $id;
return TRUE;
}
}
//获得Kind类的name
function getKindName() {
$this->setQuery("select * from $this->tableName where kind_id = '$this->idKind'");
$this->store();
$KindName = mysql_fetch_array($this->result);
return $KindName["kind_name"];
}
//获得该类的子件,555555~~,要是用PHP5该多好啊
function returnNews() {
$myNews = new news(0, $this->idKind, "NONE", "NONE");
return $myNews->returnNewsOfType();
}
}
/*测试
*/
$myKind = new kind(1);
echo $myKind->showKind();
$test = $myKind->returnNews();
echo "<pre>";
print_r($test);
echo "</pre>";
$myKind->closeDatabase();
/*
*/
?>
news.class.php
<?php
include_once("database.class.php");
class news extends database {
var $idNews = NULL;
var $typeNews = NULL;
var $titleNews = NULL;
var $textNews = NULL;
var $tableName = "web_news";
//初始化
function news() {
$this->connect();
$this->selectDatabase();
switch(func_num_args()) {
case 1 : $this->setNewsId(func_get_arg(0));
$this->getNewsInfomation();
break;
case 4 : $this->setNewsId(func_get_arg(0));
$this->setNewsType(func_get_arg(1));
$this->setNewsTitle(func_get_arg(2));
$this->setNewsText(func_get_arg(3));
break;
default :
exit;
}
}
//插入News
function insertNews() {
$this->setQuery("insert into $this->tableName set
news_type = '$this->typeNews',
news_title = '$this->titleNews',
news_text = '$this->textNews'");
$this->store();
return $this->result;
}
//修改News
function updateNews() {
$this->setQuery("update $this->tableName set
news_type = '$this->typeNews',
news_title = '$this->titleNews',
news_text = '$this->textNews'
where news_id = '$this->idNews'");
$this->store();
return $this->result;
}
//删除News
function delNews() {
$this->setQuery("delete from $this->tableName where news_id = '$this->idNews'");
$this->store();
return $this->result;
}
//返回同一标题的内容数目
function returnNumTitle() {
$this->setQuery("select * from $this->tableName where news_title = '$this->titleNews'");
$this->store();
return mysql_num_rows($this->result);
mysql_free_result($this->result);
}
//返回同一种类的子件id
function returnNewsOfType() {
$this->setQuery("select news_id from $this->tableName where news_type = '$this->typeNews'");
$this->store();
while($row = mysql_fetch_array($this->result)) {
$NewsId[] = $row["news_id"];
}
return $NewsId;
}
//设置News类id
function setNewsId($id) {
if($id < 0) {
exit;
} else {
$this->idNews = $id;
return TRUE;
}
}
//设置News类type
function setNewsType($type) {
if($type < 0) {
exit;
} else {
$this->typeNews = $type;
return TRUE;
}
}
//设置News类title
function setNewsTitle($title) {
if($title == "") {
exit;
} else {
$this->titleNews = $title;
return TRUE;
}
}
//设置News类text
function setNewsText($text) {
if($text == "") {
exit;
} else {
$this->textNews = $text;
return TRUE;
}
}
//设置News类type/title/text信息,偶比较懒^^
function getNewsInfomation() {
$this->setQuery("select * from $this->tableName where news_id = '$this->idNews'");
$this->store();
$NewsInformation = mysql_fetch_array($this->result);
$this->setNewsType($NewsInformation["news_type"]);
$this->setNewsTitle($NewsInformation["news_title"]);
$this->setNewsText($NewsInformation["news_text"]);
mysql_free_result($this->result);
}
//返回id
function returnId() {
return $this->idNews;
}
//返回type
function returnType() {
return $this->typeNews;
}
//返回title
function returnTitle() {
return $this->titleNews;
}
//返回text
function returnText() {
return $this->textNews;
}
}
/*调试
*/
$myNews = new news(3);
echo $myNews->returnNumTitle();
$test = $myNews->returnNewsOfType();
echo "<pre>";
print_r($test);
echo "</pre>";
$myNews->closeDatabase();
/*
*/
?>
数据结构
CREATE TABLE web_kind (
kind_id int(11) NOT NULL auto_increment,
kind_name varchar(50) NOT NULL default '',
PRIMARY KEY (kind_id)
) TYPE=MyISAM;
CREATE TABLE web_news (
news_id int(11) NOT NULL auto_increment,
news_type int(11) NOT NULL default '0',
news_title varchar(100) NOT NULL default '',
news_text text NOT NULL,
PRIMARY KEY (news_id)
) TYPE=MyISAM;
[B]10、后记[/B][/COLOR]
本文主要依赖PHP4,进入PHP5后,本文就是垃圾;考虑到PHP5仍需时间,所以没有采用PHP5编码。
本文的主旨是表明设计思想,自己比较懒,也不想再检查什么,看见错误就不要Q偶啦。
之所以放到这个板块,是觉得项目组如果有一个资深系统分析员带队,事情会变得很简单。既然大大不愿/不屑出面,本极品菜鸟本着“JUST DO IT”的原则,献丑了。^^
[B]11、撰写本文的真正目的^^[/B][/COLOR]
亲爱的雨伞
好久没你消息了
这两天总想你
心里很乱
寻遍你爱去的池塘
就餐的小屋
睡觉的草坪
仍不见你踪影
我心都快碎了
。
。
。
。
。
养这么大的猪,咋就丢了呢
猪,你生日快乐!^^
——开心果到此一游
2003年11月11日