Correction status:Uncorrected
Teacher's comments:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/10 0010 * Time: 10:19 */ namespace index; require 'controller/Controller.php'; use mvc\controller\Controller; $index = new Controller(); echo $index->index();
点击 "运行实例" 按钮查看在线实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/10 0010 * Time: 10:18 */ namespace mvc\controller; use mvc\model\Model; use mvc\view\View; class Controller { public function index() { //加载文件 require './model/Model.php'; //创建对象 $model = new Model('root','root','xc'); //执行查询 $model->select('blast_members',10); // 获取数据 $result = $model->result; //加载文件 require './view/View.php'; //创建对象 $view = new View($result); //获取数据 $data = $view->getData() ; //渲染到视图层 $view->display($data); } }
点击 "运行实例" 按钮查看在线实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/10 0010 * Time: 10:19 */ namespace mvc\model; class Model { public $result = []; public $pdo = null; public function __construct($user,$pass,$dbname) { try{ $this->pdo = new \PDO("mysql:host=127.0.0.1;dbname={$dbname}",$user,$pass); }catch (\PDOException $e){ return 'ERROR'.$e->getMessage(); } } //创建查询方法 public function select($table,$num) { //创建sql语句 $sql = "SELECT uid,username,nickname,regTime,score,coin FROM {$table} LIMIT 10;"; //创建预处理对象 $stmt = $this->pdo->prepare($sql); //执行预处理 $stmt->bindValue(':num',$num,\PDO::PARAM_INT); $stmt->execute(); //获取结果集 $this->result = $stmt->fetchAll(\PDO::FETCH_ASSOC); } } //$demo = new Model('root','root','xc'); //$demo->select(blast_members,10); //print_r($demo->result);
点击 "运行实例" 按钮查看在线实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/10 0010 * Time: 10:16 */ namespace mvc\view; class View { public $data = []; //构造方法用于接收并初始化数据 public function __construct($data) { $this->data = $data; } //外部获取数据提供接口 public function getData() { return $this->data; } //渲染方法 public function display($data) { $table = " <!doctype html> <html lang=\"en\"> <head> <meta charset=\"UTF-8\"> <meta name=\"viewport\" content=\"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0\"> <meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"> <script src=\"http://libs.baidu.com/jquery/2.0.0/jquery.min.js\"></script> <script src=\"../../../js/bootstrap.js\" type=\"text/javascript\" charset=\"utf-8\"></script> <link rel=\"stylesheet\" type=\"text/css\" href=\"../../../css/bootstrap.css\"/> <title>mvc设计模式</title> <style type=\"text/css\"> table{margin: 30px auto;} caption{ font-size:2em ;font-weight: bold;text-align: center; } </style> </head> <body> <div class=\"container\"> <div class=\"row\"> <div class=\"col-md-12\"> <div class=\"col-md-offset-0\"> <table class=\"table table-hover\"> <caption>用户信息表</caption> <tr class=\"info\"> <th>用户ID</th> <th>用户名称</th> <th>用户昵称</th> <th>注册时间</th> <th>用户积分</th> <th>账户余额</th> </tr> "; //循环输出数据 foreach ($data as $row){ $table .='<tr>'; $table .='<td>'.$row['uid'].'</td>'; $table .='<td>'.$row['username'].'</td>'; $table .='<td>'.$row['nickname'].'</td>'; $table .='<td>'.date('Y-m-d H:i:s',$row['regTime']).'</td>'; $table .='<td>'.$row['score'].'</td>'; $table .='<td>'.$row['coin'].'</td>'; $table .='</tr>'; } $table .='</table></div></div></div></div></body></html>'; echo $table ; } }
点击 "运行实例" 按钮查看在线实例