辖冒墙呵混叛错芬喷钝廊房撮
新建test.php 新建model目录 model下新建Model.php
Model.php
<?php namespace mvc\model; //模型类 class Model { public $pdo=null; public $result=[]; //构造方法 public function __construct($dbname,$user,$pass) { //连接数据库 $this->pdo=new PDO("mysql:host=127.0.0.1;dbname={$dbname}",$user,$pass); } //查询方法 public function select($table,$num) { $stmt=$this->pdo->prepare("select staff_id,name,age,salary from {$table} limit :num"); //为占位符绑定值 //绑定的值为10 类型为int $stmt->bindValue(':num',$num,PDO::PARAM_INT); //3.执行sql语句 $stmt->execute(); //4.获取结果集 $this->result=$stmt->fetchAll(PDO::FETCH_ASSOC); } }
test.php:
<?php require "./model/Model.php"; use mvc\model\Model; $model=new Model('myuser','root','root'); $model->select('staff',10); $result=$model->result; echo "<pre/>"; print_r($result);
执行
错误原因,Model.php声明的空间是:
namespace mvc\model;
PDO在这个空间是找不到的,需要在每个PDO前面添加\ 在全局中才能找到PDO
修改Model.php:
<?php namespace mvc\model; //模型类 class Model { public $pdo=null; public $result=[]; //构造方法 public function __construct($dbname,$user,$pass) { //连接数据库 $this->pdo=new \PDO("mysql:host=127.0.0.1;dbname={$dbname}",$user,$pass); } //查询方法 public function select($table,$num) { $stmt=$this->pdo->prepare("select staff_id,name,age,salary from {$table} limit :num"); //为占位符绑定值 //绑定的值为10 类型为int $stmt->bindValue(':num',$num,\PDO::PARAM_INT); //3.执行sql语句 $stmt->execute(); //4.获取结果集 $this->result=$stmt->fetchAll(\PDO::FETCH_ASSOC); } }
执行:
新建view目录 和View.php
View.php代码:
<?php //视图类 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"> <title>mvc设计思想</title> <style> table,td,th { /*1px 实心线 黑色*/ border:1px solid black; } table { /*折叠表格线*/ border-collapse:collapse; width:60%; margin:30px auto; text-align: center; } caption { font-size: 1.5em; margin-bottom: 15px; } /*//第一个tr添加样式*/ table tr:first-child { background-color: lightskyblue; } </style> </head> <body> <!--M Model:模型的缩写 可以认为是数据库、数据表--> <!--V View :视图 模板--> <!--C Controller:控制器 根据用户请求调度数据填充界面--> <table> <caption>员工信息表</caption> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>工资</th> </tr>'; // 遍历数据 foreach ($data as $staff) { $table.='<tr>'; $table.='<td>'.$staff['staff_id'].'</td>'; $table.='<td>'.$staff['name'].'</td>'; $table.='<td>'.$staff['age'].'</td>'; $table.='<td>'.$staff['salary'].'</td>'; $table.='</tr>'; } $table.='</table> </body> </html>'; //输出表格 echo $table; } }
test.php
<?php require "./model/Model.php"; use mvc\model\Model; $model=new Model('myuser','root','root'); $model->select('staff',10); $result=$model->result; //测试视图: require "./view/View.php"; use mvc\view\View; $view=new View($result); $data=$view->getData(); $view->display($data);
执行:
新建controller目录 新建Controller.php index.php
Controller.php
<?php namespace mvc\controller; class Controller { public function index() { require "./model/Model.php"; //从模型中获取数据 // 因为当前在mvc\controller 所以\mvc\model\Model 要使用完全限定空间\ $model=new \mvc\model\Model('myuser','root','root'); $model->select('staff',10); $result=$model->result; //从模型中获取的数据要求来渲染模板,格式化展示模型中的数据 require "./view/View.php"; $view=new \mvc\view\View($result); $data=$view->getData(); $view->display($data); } }
index.php
<?php /** * 入口文件 */ //导入控制器 require "controller/Controller.php"; use mvc\controller\Controller; $controller=new Controller(); $controller->index();
执行:
mvc源码:
http://www.xuexianswer.com/yunpan/short.php?sid=blSjz
穗杀渴膜氢阿碎傻哆垃蜕谜活