mvc{
M:模型 用来操作表的
V : 模版 用来显示页面
C:控制器 调度 操作那个表产生数据 调用哪个模版显示数据
}

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>
match_list_c.php 代码(引入)
<?php require "match_list_m.php"; require "template/match_list_v.html";
match_list_m.php 代码
<?php
include "mysql.class.php";
$config=[
"host"=>"localhost",
"name"=>"root",
"pwd"=>"",
"DbName"=>"z_075mvc"
];
$db= mysqlDb::getInstance($config);
//获得比赛列表数据
$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";
$match_list = $db->getAssoc($sql);
mysql.class.php 代码
<?php
class mysqlDb{
public $host; //服务器地址
public $post=3306; //端口
public $name; //用户名
public $pwd; //m密码
public $DbName; //数据库名称
public $charset;//默认编码
public $link;//数据库连接对象
public $res; //资源 返回结果集
//三私一公
private static $obj;//用来存newdb对象
//构造方法 私有的
private function __construct($config=array()){
/*初始化参数*/
$this->host = $config["host"] ? $config["host"] : "localhost";
$this->name = $config["name"] ? $config["name"] : "root";
$this->pwd = $config["pwd"] ? $config["pwd"] : "";
$this->DbName = $config["DbName"] ? $config["DbName"] : "mysql";
$this->charset = $config["charset"] ?$config["charset"] : "utf8" ;
/*连接数据库 设置字符集*/
$this->connectMysql();
$this->setCharSet();
}
//私有化clone方法
private function __clone(){}
//提供公共的返回对象方法
static function getInstance($config=array()){
if(!isset(self::$obj)){
self::$obj=new self($config);
}
return self::$obj;
}
/*连接数据库*/
function connectMysql(){
$this->link = new MySQLi($this->host,$this->name,$this->pwd,$this->DbName);
!mysqli_connect_error() or die("连接数据库失败");
}
/*设置字符集*/
function setCharSet(){
$this->link->query("set names ".$this->charset );
}
/*执行sql语句的方法*/
function DbQuery($sql){
$this->res = $this->link->query($sql);
if(!$this->res){
echo ("<br />执行失败。");
echo "<br />失败的sql语句为:" . $sql;
echo "<br />出错信息为:" . mysqli_error($this->link);
echo "<br />错误代号为:" . mysqli_errno($this->link);
die;
}
return $this->res;
}
//返回字符串
function getStr($sql){
$zhi = $this->DbQuery($sql);
//将结果集转为字符串
$arr = $zhi->fetch_all();
$brr = array();
foreach($arr as $v){
$brr[] = implode(",",$v);
}
return implode("^",$brr);
}
//返回json
function getJson($sql){
$zhi = $this->DbQuery($sql);
$brr = array();
while($row = $zhi->fetch_assoc()){
$brr[] = $row;
}
return json_encode($brr);
}
/*返回关联数组*/
function getAssoc($sql){
$zhi = $this->DbQuery($sql);
$brr = array();
while($row = $zhi->fetch_assoc()){
$brr[] = $row;
}
return $brr;
}
//返回索引数组
function getAttr($sql){
$zhi = $this->DbQuery($sql);
return $zhi->fetch_all();
}
}
展示效果图
