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();
运行实例 »
点击 "运行实例" 按钮查看在线实例