首先有根目录match文件夹
match文件夹
template文件夹(放视图文件,就是看到的)
match.html(在template下)
match.php(主页面,中专用的)
mysql.class.php(连接数据库类文件)
Factory.class.php(工厂文件)
MatchController.class.php(控制器文件)
model.class.php(模型文件)
MatchModel.class.php(模型子文件)
match.html:
<table width="100%" border="1"> <tr> <th>队伍一</th> <th>比分</th> <th>队伍二</th> <th>时间</th> </tr> <?php foreach($arr as $k=>$v){?> <tr> <td><?php echo $v['t1_name'];?></td> <td><?php echo $v['t1_score'].' : '.$v['t2_score'];?></td> <td><?php echo $v['t2_name'];?></td> <td><?php echo date('Y-m-d',$v['m_time']);?></td> </tr> <?php } ?> </table>
match.php:
<?php /*引入工厂*/ require "./Factory.class.php"; /*把控制器应用到工厂*/ $matchContro=Factory::M('MatchController'); /*调用工厂里的放入数据方法*/ $matchContro->getList();
mysql.class.php
<?php class mysql{ public $host;//服务器地址 public $username;//数据库账号 public $password;//数据库密码 public $dbname;//数据库名称 public $charset;//数据库字符集 public $db;//mysqli的数据库连接对象 /*跟js页面加载一样,给数据赋值,就是页面加载干什么事*/ function __construct($config = []){//__construct初始化结构,用来初始化数据的 $this->host=isset($config['host'])?$config['host']:'localhost'; $this->username=isset($config['username'])?$config['username']:'root'; $this->password=isset($config['password'])?$config['password']:'root'; $this->dbname=isset($config['dbname'])?$config['dbname']:'z_0222'; $this->charset=isset($config['charset'])?$config['charset']:'utf8'; /*连接数据库*/ $this->mysqlConnect(); /*设置字符集*/ $this->setCharset(); } /*连接数据库方法*/ function mysqlConnect(){ $this->db=new mysqli($this->host,$this->username,$this->password,$this->dbname); !mysqli_connect_error() or die("数据库连接失败"); } /*设置字符集的方法*/ function setCharset(){ $this->db->query('set names '.$this->charset); } /*执行sql语句,如果成功返回执行结果,失败直接结束*/ public function query($sql){ if(!$result=$this->db->query($sql)){ echo ("<br/>执行失败。"); echo "<br/>失败的sql语句为:".$sql; echo "<br/>出错信息为:".mysqli_error($this->db); echo "<br/>错误代号为:".mysqli_errno($this->db); die(); } return $result; } /*获取二维索引数组*/ function getAll($sql){ $result=$this->query($sql); return $result->fetch_all(); } /*获取二维关联数组*/ function getAssoc($sql){ $result=$this->query($sql); $arr=[]; while($row=$result->fetch_assoc()){ $arr[]=$row; } return $arr; } /*获取一维索引数组*/ function getNum($sql){ $result=$this->query($sql); return $result->fetch_assoc(); } /*获取一维关联数组*/ function getOne($sql){ $result=$this->query($sql); return $result->fetch_row(); } /*获取第一个数组元素*/ function getFrist($sql){ $result=$this->query($sql); $row=$result->fetch_row(); return $row[0]; } /*获取关联数组第一个元素*/ function getFristAssoc($sql){ $result=$this->query($sql); $assoc=$result->fetch_assoc(); return $assoc[0]; } }
Factory.class.php:
<?php
class Factory{ /*定义一个静态方法*/ 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]; } }
MatchContorller.class.php:
<?php /* 比赛操作相关控制器功能类*/ class MatchController{ /* 获取比赛列表*/ public function getList(){ //实例化相应模型类对象,调用某方法,实现固定功能 //通过工厂获得对象 $match=Factory::M('MatchModel'); $arr=$match->getList(); //载入负责显示的html文件 require './template/match.html'; } }
model.class.php:
<?php class Model{ protected $db; function __construct(){ $this->db=$this->getDb(); } function getDb(){ include "mysql.class.php"; $config=[ 'host'=>'localhost', //服务器地址 'username'=>'root', //用户名 'password'=>'root', //用户密码 'dbname'=>'mvc', //数据库名 'charset'=>'utf8' //字符集 ]; return new mysql($config); } }
MatchModel.class.php:
<?php include "./model.class.php"; class MatchModel extends Model{ 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"; $arr=$this->db->getAssoc($sql); return $arr; }
}
不要太在意数据库数据,重要的是php运转的逻辑