<?php /**home\index.php * Created by PhpStorm. * User: 1A7489 * Date: 2018/5/14 * Time: 14:16 */ require 'controller/Controller.php';//导入控制器 use Home\controller\Controller;//导入类 $controller = new Controller;//实例化对类 $controller->index();//运行导入的Controller类中的index方法 ?>
<?php /**home\Controller \Controller .php * Created by PhpStorm. * User: 1A7489 * Date: 2018/5/14 * Time: 14:08 */ namespace Home\controller; class Controller { public function index() { require './model/Model.php';//导入模型 $model = new \Home\model\Model('php','root','root');//实例化对类 $model -> select('staff',10);//设置sql属性,传入参数,执行select函数 $result = $model -> result;//保存select函数的结果集到$result echo print_r($result).'<br>我是$result'; echo '<hr>'; require './view/View.php';//导入视图 $view = new \Home\view\View($result); //实例化对类,并将自动调用 __construct() ,将拿到的结果集传给视图中$data $data = $view->getData(); //执行getData函数,将返回$data中HTML代码(结果集)保存到变量$data echo print_r($data).'<br>我是$data'; $view->display($data); //执行函数dispaly,传入结果集,输出完整html代码 require './view/View1.php';//导入视图 $view = new \Home\view\View1(); //实例化对类,并将自动调用 __construct() ,将拿到的结果集传给视图中$data //$data = $view->getData(); //执行getData函数,将返回$data中HTML代码(结果集)保存到变量$data $view->display($result); //执行函数dispaly,传入结果集,输出完整html代码 } } ?>
<?php /**home\Model\Model.php * Created by PhpStorm. * User: 1A7489 * Date: 2018/5/14 * Time: 14:08 */ namespace Home\Model; class Model { public $pdo = null; public $result = []; //构造方法 public function __construct($dbname, $user, $pass) { //连接数据库 $this->pdo = new \PDO('mysql:host=localhost;dbname='.$dbname,$user,$pass); } //执行查询并获取结果集 public function select($table, $num) { //创建预处理语句:将复制过来的 $pdo 替换成 $this->pdo $stmt = $this->pdo->prepare("SELECT staff_id,name,age,salary FROM {$table} LIMIT :num;"); //执行查询 $stmt -> bindValue(':num',$num,\PDO::PARAM_INT); $stmt -> execute(); //解析结果集:将复制过来的 $result 替换成成 $this->result $this->result = $stmt -> fetchAll(\PDO::FETCH_ASSOC); } }
<?php //home\View\View.php namespace Home\view; class View { public $data = []; public function __construct($data) { $this->data = $data;//将结果集保存到类中的$data } public function getData() { return $this->data;//返回html代码 } public function display($data) { $table = ' <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>MVC模式简介</title> <style type="text/css"> table,th,td { border:1px solid black; } table { border-collapse: collapse; width: 60%; margin: 30px auto; text-align: center; } table tr:first-child { background-color: lightgreen; } table caption { font-size:1.5em; margin-bottom: 15px; } </style> </head> <body> <table> <caption>员工信息表</caption> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>工资</th> </tr>'; foreach ($data as $key) {//输出结果集中的数据(数组)到变量$key进行遍历,foreach 仅能够应用于数组和对象 $table .= '<tr>'; $table .= '<td>'.$key['staff_id'].'</td>'; $table .= '<td>'.$key['name'].'</td>'; $table .= '<td>'.$key['age'].'</td>'; $table .= '<td>'.$key['salary'].'</td>'; } $table .= '</table> </body> </html>'; echo $table;//输出html代码 } }
<?php //home\View1\View1.php namespace Home\view1; class View1 { public $data = []; public function display($data) { $table = ' <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>MVC模式简介</title> <style type="text/css"> table,th,td { border:1px solid black; } table { border-collapse: collapse; width: 60%; margin: 30px auto; text-align: center; } table tr:first-child { background-color: lightgreen; } table caption { font-size:1.5em; margin-bottom: 15px; } </style> </head> <body> <table> <caption>员工信息表</caption> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>工资</th> </tr>'; foreach ($data as $key) {//输出结果集中的数据(数组)到变量$key进行遍历,foreach 仅能够应用于数组和对象 $table .= '<tr>'; $table .= '<td>'.$key['staff_id'].'</td>'; $table .= '<td>'.$key['name'].'</td>'; $table .= '<td>'.$key['age'].'</td>'; $table .= '<td>'.$key['salary'].'</td>'; } $table .= '</table> </body> </html>'; echo $table;//输出html代码 } }
学习mvc发现view代码中的结果集转了一圈又回来了,于是动手简化,发现输出的结果是一样的,不知道有没有错误或是其他的影响