使用mvc做页面展示
数据表customer
目录
Db.php
<?php
namespace mvc1\model;
use PDO;
class Db
{
private $dbConfig = [
'db'=>'mysql',
'host'=>'localhost',
'username'=>'root',
'password'=>'root',
'dbname'=>'mydb'
];
private static $instance = null;
private $conn = null;
private function __construct($params=[]){
//初始化参数
$this->dbConfig = array_merge($this->dbConfig,$params);
//连接数据库
$this->connect();
}
//禁止克隆
private function __clone(){
}
//创建唯一实例
public static function getInstance($params){
if(!self::$instance instanceof self){
self::$instance = new self($params);
}
return self::$instance;
}
private function connect(){
//连接数据库
try{
$dsn = "{$this->dbConfig['db']}:host={$this->dbConfig['host']};dbname={$this->dbConfig['dbname']}";
//PDO连接对象
$this->conn = new PDO($dsn,$this->dbConfig['username'],$this->dbConfig['password']);
}catch(PDOException $e){
die('数据库连接失败'.$e->getMessage());
}
}
//查询全部数据
public function selectAll($sql){
return $this->conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
}
Model.php
<?php
namespace mvc1\model;
include 'Db.php';
class Model
{
protected $db = null;
public $data = null;
public function __construct(){
$this->init();
}
private function init(){
$dbConfig = [
'username'=>'root',
'password'=>'12341234',
'dbname'=>'php_2019',
];
//Db类实例
$this->db = Db::getInstance($dbConfig);
}
//获取数据表全部数据
public function getAll($tableName){
$sql = "SELECT * FROM .$tableName;";
return $this->data = $this->db->selectAll($sql);
}
}
CustomerModel.php
<?php
namespace mvc1\model;
require 'Model.php';
class CustomerModel extends Model
{
private $cid;
private $name;
private $password;
private $sex;
private $birth;
private $level;
private $is_vip;
private $createtime;
private $updatetime;
private $status;
private $del;
public function __get($field){
return $this->$field;
}
public function __set($field,$value){
$this->$field = $value;
}
public function __construct(){
parent::__construct();
//自动转换 时间日期
$this->birth = date('Y-m-d' , $this->birth);
$this->createtime = date('Y-m-d' , $this->createtime);
$this->updatetime = date('Y-m-d' , $this->updatetime);
//自动转换 性别
if($this->sex == 1 || $this->sex == 0){
$this->sex = $this->sex ? '男' : '女';
}else{
$this->sex = '未知';
}
}
}
CustomerController.php
<?php
namespace mvc1\controller;
use mvc1\model\CustomerModel;
use mvc1\view\CustomerView;
class CustomerController
{
protected $customerModel;
protected $customerView;
public function __construct(customerModel $customerModel,customerView $customerView){
$this->customerModel = $customerModel;
$this->customerView = $customerView;
}
//首页
public function index(){
$data = $this->customerModel->getAll('customer');
return $this->customerView->show($data);
}
}
CustomerView.php
<?php
namespace mvc1\view;
class CustomerView
{
public function show($data){
$table = '<table>';
$table .= '<caption>客户信息表</caption>';
$table .= '<tr><th>ID</th><th>客户</th><th>性别</th><th>生日</th><th>等级</th><th>vip</th><th>创建时间</th></tr>';
foreach ($data as $customer){
$table .='<tr>';
$table .='<td>'. $customer['cid'] .'</td>';
$table .='<td>'. $customer['name'] .'</td>';
$table .='<td>'. $customer['sex'] .'</td>';
$table .='<td>'. date('Y-m-d' ,$customer['birth']) .'</td>';
$table .='<td>'. $customer['level'] .'</td>';
$table .='<td>'. $customer['is_vip'] .'</td>';
$table .='<td>'. date('Y-m-d' ,$customer['createtime']) .'</td>';
$table .='</tr>';
}
$table .= '</table>';
return $table;
}
}
echo '
<style>
table {
border-collapse: collapse;
border: 1px solid;
width: 500px;
height: 150px;
margin:auto;
}
caption {
font-size:1.2rem;
margin-bottom: 10px;
}
tr:first-of-type{
background-color:lightblue;
}
td,th{
border:1px solid
}
td:first-of-type{
text-align:center;
}
</style>
';
autoload.php
<?php
spl_autoload_register(function($className){
$path = str_replace('\\', '/', $className);
require dirname(__DIR__).DIRECTORY_SEPARATOR.$path.'.php';
});
index.php
<?php
require __DIR__.'/autoload.php';
use mvc1\model\CustomerModel;
use mvc1\view\CustomerView;
use mvc1\controller\CustomerController;
//客户端调用
$customerModel = new CustomerModel();
$customerView = new CustomerView();
$customerController = new CustomerController($customerModel,$customerView);
echo $customerController->index();
页面展示
总结:自己要写一个小框架给自己用还是挺难实现的。脑子里有想法又很乱。只做了基于mvc的展示,很多功能都没有做。