Factory.class.php
<?php
/**
*
*项目中的工厂类
*
*/
class Factory{
/**
*生成模型的单列对象
*
*@param $model_name string
* @return object
*
*/
static function M($model_name){
static $model_list=array();//存储已经实例化好的模型对象的列表,下标模型名,值模型对象
//判断当前模型是否已经实例化
if(!isset($model_list[$model_name])){
//没有实例化过
require './' . $model_name . '.class.php';
$model_list[$model_name]=new $model_name;//可变标志符,可变类
}
return $model_list[$model_name];
}
}
Model.class.php
<?php
/**
* 基础模型类
*/
class Model{
protected $_dao;//存储DAO对象的属性,可以在子类方法中被访问,使用DAO对象
/**
* 初始化DAO
*/
public function __construct(){
$this->getDao();
}
public function getDao(){
//初始化MySQLDB
$config=array(
'host'=>'localhost',
'port'=>'3306',
'username'=>'root',
'password'=>'root',
'charset'=>'utf8',
'dbname'=>'php34'
);
require './MYSQLDB.class.php';
//$dao , Database Access Object 数据库操作对象(dao层)
$this->_dao=MySQLDB :: getInstance($config);
}
}
MatchModel.class.php
<?php
/**
* match表的操作模型类
*/
require './Model.class.php';
class MatchModel extends Model{
/**
* 获得所有的比赛列表
*/
public function getList(){
//获得比赛列表数据
$sql="SELECT t1.`t_name` AS t1_name,m.`t1_score`,m.`t2_score`,t2.`t_name` AS t2_name,m.`m_time` FROM `match` AS m LEFT JOIN team AS t1 ON m.`t1_id`=t1.`t_id` LEFT JOIN team AS t2 ON m.`t2_id`=t2.`t_id`;";
return $this->_dao->getAll($sql);
}
/**
* 删除某场比赛
*/
public function removeMatch($m_id) {
$sql = "delete from `match` where m_id=$m_id";
return $this->_dao->query($sql);
}
public function rmTeam($t_id) {
return $this->_dao->query("delete from `team` where t_id = $t_id");
}
}
match_list_c.php
<?php
//比赛列表
header('Content-Type:text/html;charset=utf-8');
// 实例化相应的模型类对象,调用某个方法,实现固定功能
// require './MatchModel.class.php';
// $m_match = new MatchModel();
//通过工厂获得对象
require './Factory.class.php';
$_match=Factory::M('MatchModel');
$match=$_match->getList();
// 载入负责显示的html文件
require './template/match_list_v.html';
match_list_v.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>比赛列表</title>
</head>
<body>
<table>
<tr>
<th>球队一</th><th>比分</th><th>球队二</th><th>比赛时间</th>
</tr>
<?php foreach($match as $row):?>
<tr>
<td><?php echo $row['t1_name']?></td>
<td><?php echo $row['t1_score']?>:<?php echo $row['t2_score']?></td>
<td><?php echo $row['t2_name']?></td>
<td><?php echo date('Y-m-d H:i ',$row['m_time'])?></td>
</tr>
<? endforeach ?>
</table>
</body>
</html>
.htaccess
Deny from All