Correction status:qualified
Teacher's comments:好吧, MVC流程理解了就好
Model.php
<?php /** 模型类 // */ class Model { public $stmp; private $pdo=null; public $data=array(); public function getData() { $this->pdo = new PDO('mysql:host=127.0.0.1;dbname=php', 'root', 'root'); $stmp = $this->pdo->prepare('SELECT * FROM `user` WHERE id>1 '); $stmp->execute(); return $data=$stmp->fetchAll(PDO::FETCH_ASSOC); } }
点击 "运行实例" 按钮查看在线实例
View.php
<?php class View { public function fetch($data) { $table = '<table border="1" cellspacing="0" cellpadding="3" width="80%">'; $table .= '<caption>用户信息表</caption>'; $table .= '<tr bgcolor="#add8e6"><th>ID</th><th>姓名</th><th>年龄</th></tr>'; // 遍历模型数据 foreach ($data as $row) { $table .= '<tr>'; $table .= '<td>' . $row['id'] . '</td>'; $table .= '<td>' . $row['name'] . '</td>'; $table .= '<td>' . $row['age'] . '</td>'; $table .= '</tr>'; } $table .= '</table>'; return $table; } }
点击 "运行实例" 按钮查看在线实例
controller.php
<?php require 'Model.php'; require 'View.php'; class Controller { public function index() { $model=new Model(); $data = $model->getData(); $view = new View(); return $view->fetch($data); } } $controller = new Controller(); echo $controller-> index();
点击 "运行实例" 按钮查看在线实例