批改状态:合格
老师批语:
1,Model控制器
<?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($tbale,$num)
{
$sql = "SELECT `id`,`name`,`email`,`age` FROM {$tbale} LIMIT {$num}";
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(':num',$num,\PDO::PARAM_INT);
$stmt->execute();
$this->result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
}点击 "运行实例" 按钮查看在线实例
2、VIEW视图
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/17
* Time: 0:31
*/
namespace mvc\view;
class View
{
public $data = [];
public function __construct($data)
{
$this->data = $data;
}
public function getdata()
{
return $this->data;
}
public function Rendering($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>员工表</title>
</head>
<body>
<div class="box">
<table>
<tr>
<td>ID</td>
<td>姓名</td>
<td>邮箱</td>
<td>年龄</td>
</tr>';
foreach ($data as $staff) {
$table .= '<tr>';
$table .= '<td>'.$staff['id'].'</td>';
$table .= '<td>'.$staff['name'].'</td>';
$table .= '<td>'.$staff['email'].'</td>';
$table .= '<td>'.$staff['age'].'</td>';
$table .= '</tr>';
}
$table .='</table></div></body></html>';
echo $table;
}
}点击 "运行实例" 按钮查看在线实例
3、CONTROLLER设置器
<?php
namespace mvc\controller;
use mvc\model\Model;
use mvc\view\View;
class Controller
{
public function inde()
{
require './model/Model.php';
$model = new Model('php','root','root');
$model->select('user',10);
$result = $model->result;
require './view/View.php';
$view = new View($result);
$data = $view->getdata();
$view->Rendering($data);
}
}点击 "运行实例" 按钮查看在线实例
4、首页入口文件
<?php use mvc\controller\Controller; require './controller/Controller.php'; $controller = new Controller; $controller->inde();
点击 "运行实例" 按钮查看在线实例
总结:在写做的过程中不是很仔细,对于命名空间还是不了解,因为当中出现了很多错误。还有就是对对象的了解还是非常的模糊,还要加强学习。总体来说,能够把一些问题搞懂也是非常的不容易。
MVC设计的思想:我觉得MVC设计的思想就是要把代码做得更好的管理,把数据处理,页面效果通过设置器来来调协 ,然后通过调用的方法。第一是让程序更好的重复使用,第二就是为了更好的管理,第三就是为安全方面着想。我觉得是这样。我相信MVC的作用肯定还有更大的用处。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号