0、唠叨
好久没有更新博客了,这学期上课也没有状态,不知道是临近毕业了,还是因为一些事情影响了心情。总之心态不太对吧,大学里面做的一些项目 好好坏坏也都上线使用了,但是过程我很狼狈,呵呵。深知团队的重要性,文笔不好,口才不好,也就只适合做编程了吧!哈哈。今天突然发现这学期php 我一节课没听过,快要陌生了,就写了一个小例子,没有过多的注释,就算是记录吧!
1、数据库连接类
<?php
/**
* 连接数据库
* User: qiangzi
* Date: 2017/3/22
* Time: 10:31
*/
class MySqlPDO{
private $dbConfig=array( //数据库配置数组
'db'=>'mysql',
'host'=>'localhost',
'port'=>'3306',
'user'=>'root',
'pass'=>'123456789',
'charset'=>'utf8',
'dbname'=>'',
);
private static $instance;
private $db;
private function __construct($params)
{
$this->dbConfig=array_merge($this->dbConfig,$params);
$this->connect();
}
public static function getInstance($params=array()){
if(!self::$instance instanceof self){
self::$instance = new self($params);
}
return self::$instance;
}
private function connect(){
try{
$dns="{$this->dbConfig['db']}:host={$this->dbConfig['host']};port={$this->dbConfig['port']};dbname={$this->dbConfig['dbname']};charset={$this->dbConfig['charset']};";
$this->db=new PDO($dns,$this->dbConfig['user'],$this->dbConfig['pass']);
$this->db->query("set names {this->dbConfig['charset']}");
}catch(PDOException $e){
die("数据库操作失败:{$e->getMessage()}");
}
}
public function query($sql){
$res=$this->db->query($sql);
if($res===false){
$error=$this->db->errorInfo();
die("数据库操作失败:ERROR{$error[1]}($error[0]):{$error[2]}");
}
return $res;
}
public function fetchRow($sql){
return $this->query($sql)->fetch(PDO::FETCH_ASSOC);
}
public function fetchAll($sql){
return $this->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
}
2、model基类(M)
/**
* Created by PhpStorm.
* User: qiangzi
* Date: 2017/4/5
* Time: 10:56
*/
class model
{
protected $db;
public function __construct()
{
$this->initDB();
}
public function initDB(){
$dbConfig=array('user'=>'root','pass'=>'123456','dbname'=>'php');
$this->db=MySqlPDO::getInstance($dbConfig);
}
}
2.1 model类
<?php
/**
* Created by PhpStorm.
* User: qiangzi
* Date: 2017/4/5
* Time: 11:03
*/
class student extends model
{
//获取所有列,调用的MySqlPDO fetchAll方法
public function getAll(){
$data=$this->db->fetchAll('select * from student');
return $data;
}
}
3、controller(C)
/**
* Created by PhpStorm.
* User: qiangzi
* Date: 2017/4/5
* Time: 11:09
*/
class studentController{
public function listAction(){
$stu= new student();
$studata= empty($stu->getAll())?array():$stu->getAll();//如果无数据,返回空数据
require'./view/student.html';
}
}
4、view(V)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<?php foreach($studata as $stu): ?>
<tr>
<td><?php echo $stu['id']?></td>
<td><?php echo $stu['name']?></td>
<td><?php echo $stu['gender']?></td>
<td><?php echo $stu['age']?></td>
</tr>
<?php endForeach;?>
</table>
</body>
</html>
5、路由(算是C层,接收返回用户交互)
<?php
/**
* Created by PhpStorm.
* User: qiangzi
* Date: 2017/4/5
* Time: 11:28
*/
require 'model/model.php';//加载model
require 'model/student.php';
require 'config/MySqlPDO.php';//加载数据连接
require 'controller/studentController.php';//加载控制层
$controller= new studentController();
$controller->listAction();
6、碰到的问题
6.1在视图文件中使用 PHP 替代语法
<?php foreach($studata as $stu): ?>
<tr>
<td><?php echo $stu['id']?></td>
<td><?php echo $stu['name']?></td>
<td><?php echo $stu['gender']?></td>
<td><?php echo $stu['age']?></td>
</tr>
<?php endForeach;?>
foreach() 后面是: 结束循环标志是 endForeach
6.2 文件加载规则
先看下项目结构,如下图
如控制层要跳转到页面,我们要加载页面的路径,由于studentController.php 与 student.html 为同级子目录,所以加载 路径就是 ‘./view/student.html’