./Factory.class.php ./match_list_c.php ./MatchModel.class.php ./Model.class.php ./MySQLDB.class.php ./template/.htaccess Deny from All ./template/matc ...
-- 比賽 create table `match` ( m_id int unsigned primary key auto_increment, t1_id int unsigned comment '球隊一ID', t2_id int unsigned comment '球隊二ID', t1_score int comment '球隊一進球', t2_score int comment '球隊二進球', m_time int comment '比賽時間 時間戳' )charset=utf8; insert into `match` values (null, 3, 4, 1, 2, unix_timestamp('2015-01-31 17:00:00')), (null, 1, 2, 2, 3, unix_timestamp('2015-01-30 17:00:00')), (null, 4, 2, 2, 0, unix_timestamp('2015-01-27 17:00:00')), (null, 3, 1, 2, 0, unix_timestamp('2015-01-26 17:00:00')), (null, 5, 4, 0, 2, unix_timestamp('2015-01-22 18:30:00')), (null, 8, 5, 0, 1, unix_timestamp('2015-01-10 17:00:00')), (null, 5, 7, 2, 1, unix_timestamp('2015-01-14 17:00:00')), (null, 5, 6, 2, 1, unix_timestamp('2015-01-18 17:00:00')); -- 球隊 create table `team` ( t_id int unsigned primary key auto_increment, t_name varchar(20) )charset=utf8; insert into `team` values (1, '伊拉克'), (2, '阿聯酋'), (3, '南韓'), (4, '澳大利亞'), (5, '中國'), (6, '北韓'), (7, '烏茲別克'), (8, '沙特'); -- 運動員 create table `player` ( p_id int unsigned primary key auto_increment, p_name varchar(20), t_id int unsigned comment '球隊ID' )charset=utf8; insert into `player` values (null, '張琳芃', 5),(null, '郜林', 5),(null, '孫可', 5),(null, '王大雷', 5),(null, '吳曦', 5) ,(null, '於海', 5); select * from `match` ; select t1.t_name, m.t1_score, m.t2_score, m.t2_id, m.m_time from `match` as m left join `team` as t1 ON m.t1_id = t1.t_id; select t1.t_name, m.t1_score, m.t2_score, t2.t_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; 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;
./Factory.class.php
<?php /** * 項目中的工廠類 */ class Factory { /** * 生成模型的單例對象 * * @param $model_name string * @return object */ public 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]; } }
./match_list_c.php
<?php # 比賽列表 date_default_timezone_set('PRC'); header('Content-Type: text/html; charset=utf-8'); // 實例化相應的模型類對象,調用某個方法,實現固定功能 // require './MatchModel.class.php'; // $m_match = new MatchModel(); //通過工廠獲得對象 require './Factory.class.php'; $m_match = Factory::M('MatchModel'); $match_list = $m_match->getList(); // $m_match2 = Factory::M('MatchModel'); // 載入負責顯示的html文件 require './template/match_list_v.html';
./MatchModel.class.php
<?php /** * match表的操作模型類 */ require_once './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"); } }
./Model.class.php
<?php /** * 基礎模型類 */ class Model { protected $_dao;//存儲DAO對象的屬性,可以在子類方法中被訪問,使用DAO對象 /** * 初始化DAO */ protected function _initDAO() { //初始化MySQLDB $config = array('host' => '127.0.0.1', 'port' => '3306', 'username'=>'root', 'password' => 'h0000dh@', 'charset'=>'utf8', 'dbname'=>'itcast'); require_once './MySQLDB.class.php'; $this->_dao = MySQLDB::getInstance($config);//$dao , Database Access Object 資料庫操作對象(dao層) } /** * 構造方法 */ public function __construct() { // 初始化DAO $this->_initDAO(); } }
./MySQLDB.class.php
<?php //類名,也習慣上(推薦)使用跟文件名相似的名字 //定義一個mysql連接類,該類可以連接mysql資料庫 //並實現其單例模式 //該類的功能還能夠完成如下基本mysql操作: //執行普通的增刪改非返回結果集的語句 //執行select語句並可以返回3種類型的數據: //多行結果(二維數組),單行結果(一維數組) //單行單列(單個數據) class MySQLDB{ public $host; public $port; public $username; public $password; public $charset; public $dbname; //連接結果(資源) private static $link; private $resourc; public static function getInstance($config){ if(!isset(self::$link)){ self::$link = new self($config); } return self::$link; } //構造函數:禁止new private function __construct($config){ //初始化數據 $this->host = isset($config['host']) ? $config['host'] : 'localhost'; $this->port = isset($config['port']) ? $config['port'] : '3306'; $this->username = isset($config['username']) ? $config['username'] : 'root'; $this->password = isset($config['password']) ? $config['password'] : ''; $this->charset = isset($config['charset']) ? $config['charset'] : 'utf8'; $this->dbname = isset($config['dbname']) ? $config['dbname'] : ''; //連接資料庫 $this->connect(); //設定連接編碼 $this->setCharset($this->charset); //選定資料庫 $this->selectDb($this->dbname); } //禁止克隆 private function __clone(){} //這裡進行連接 public function connect(){ $this->resourc = mysql_connect("$this->host:$this->port", "$this->username","$this->password") or die("連接資料庫失敗!"); } public function setCharset($charset){ mysql_set_charset($charset, $this->resourc); } public function selectDb($dbname){ mysql_select_db($dbname, $this->resourc); } /** * 功能:執行最基本(任何)sql語句 * 返回:如果失敗直接結束,如果成功,返回執行結果 */ public function query($sql){ if(!$result = mysql_query($sql, $this->resourc)) { echo ("<br />執行失敗。"); echo "<br />失敗的sql語句為:" . $sql; echo "<br />出錯信息為:" . mysql_error(); echo "<br />錯誤代號為:" . mysql_errno(); die(); } return $result; } /** * 功能:執行select語句,返回2維數組 * 參數:$sql 字元串類型 select語句 */ public function getAll($sql){ $result = $this->query($sql); $arr = array(); //空數組 while( $rec = mysql_fetch_assoc( $result )){ $arr[] = $rec;//這樣就形成二維數組 } return $arr; } //返回一行數據(作為一維數組) public function getRow($sql){ $result = $this->query($sql); //$rec = array(); if( $rec2 = mysql_fetch_assoc( $result )){//返回下標為欄位名的數組 //如果fetch出來有數據(也就是取得了一行數據),結果自然是數組 return $rec2; } return false; } //返回一個數據(select語句的第一行第一列) //比如常見的:select count(*) as c from XXX where ... public function getOne($sql){ $result = $this->query($sql); $rec = mysql_fetch_row($result);//返回下標為數字的數組,且下標一定是0,1,2, 3..... //如果沒有數據,返回false if($result === false){ return false; } return $rec[0]; //該數組的第一項。 } }
./template/.htaccess
Deny from All
./template/match_list_v.html
<!-- 模板文件,利用HTML代碼展示數據 --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>比賽列表</title> </head> <body> <table> <tr> <th>球隊一</th><th>比分</th><th>球隊二</th><th>時間</th> </tr> <?php foreach($match_list 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> <?php endForeach;?> </table> </body> </html>