Blogger Information
Blog 34
fans 2
comment 0
visits 23154
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月6号作业 MVC小案例
遗忘了寂寞
Original
553 people have browsed it

文件目录

controller/StaffsController.php

  1. <?php
  2. // 控制器
  3. class StaffsController
  4. {
  5. //获取全部数据
  6. public function listAll()
  7. {
  8. //实例化模型,获取数据
  9. $sta = new StaffsModel();
  10. $data = $sta->getAll();
  11. require 'view/StaffslistAll.php';
  12. }
  13. //获取单条数据
  14. public function info($id=1)
  15. {
  16. $id = isset($_GET['id']) ? $_GET['id'] : $id;
  17. $sta = new StaffsModel();
  18. $data = $sta->get($id);
  19. require 'view/Staffslistinfo.php';
  20. }
  21. }

model/db.php

  1. <?php
  2. //数据库的基本操作
  3. class Db
  4. {
  5. //数据库的基本参数
  6. private $dbConfig=[
  7. 'type' => 'mysql', //数据库类型
  8. 'host' => 'localhost', //服务器地址
  9. 'dbname' => 'tj_ys', //数据库名称
  10. 'username' => 'root', //用户名
  11. 'password' => '123123' //密码
  12. ];
  13. //单例模式,保存本类实例
  14. private static $instance = null;
  15. //保存数据库连接实例
  16. private static $conn = null;
  17. //新增ID
  18. public $insertID = null;
  19. //受影响的数量
  20. public $num = 0;
  21. //构造方法私有化,
  22. private function __construct($params)
  23. {
  24. //初始化连接参数
  25. $this->dbConfig = array_merge($this->dbConfig,$params);
  26. //连接数据库
  27. $this->connect();
  28. }
  29. //克隆方法私有化,
  30. private function __clone()
  31. {
  32. }
  33. //获取当前类的单一实例
  34. public static function getInstance($params=[])
  35. {
  36. if(!self::$instance instanceof self){
  37. self::$instance=new self($params);
  38. }
  39. return self::$instance;
  40. }
  41. //数据库连接
  42. private function connect()
  43. {
  44. try {
  45. //连接参数
  46. $dsn = "{$this->dbConfig['type']}:host={$this->dbConfig['host']};dbname={$this->dbConfig['dbname']}";
  47. //数据库连接
  48. $this->conn = new PDO($dsn, $this->dbConfig['username'], $this->dbConfig['password']);
  49. } catch (PDOException $e) {
  50. die('连接失败' . $e->getMessage());
  51. }
  52. }
  53. //数据表的写操作,新增、更新、删除
  54. //返回受影响的记录数,如果是新增返回新增ID
  55. public function exec($sql)
  56. {
  57. $num = $this->conn->exec($sql);
  58. if ($num > 0){
  59. // 如果是新增返回新增ID
  60. if ($this->conn->lastInsertId() !== null){
  61. $insertID = $this->conn->lastInsertId();
  62. }
  63. $this->num = $num; //返回受影响的数量
  64. }else{
  65. $error = $htis->conn->errorInfo(); //获取最后操作的错误信息的数组
  66. //[0]:错误标识符;[1]:错误代码;[2]:错误信息
  67. print '操作失败' . $error[0] . ': ' . $error[1] . ': ' . $error[2];
  68. }
  69. }
  70. //查询操作:获取单条数据
  71. public function fetch($sql)
  72. {
  73. return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC);
  74. }
  75. //查询操作:获取多条数据
  76. public function fetchAll($sql)
  77. {
  78. return $this->conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);
  79. }
  80. }

model/Model.php

  1. <?php
  2. //公共模型
  3. class Model
  4. {
  5. protected $db = null; //数据库连接对象
  6. public $data = null; //当前数据内容
  7. public function __construct()
  8. {
  9. $this->init(); //完成数据连接
  10. }
  11. private function init()
  12. {
  13. $dbConfig=[
  14. 'dbname' => 'tj_ys', //数据库名称
  15. 'username' => 'root', //用户名
  16. 'password' => '123123'
  17. ];
  18. $this->db = Db::getInstance($dbConfig);
  19. }
  20. //获取全部数据
  21. public function getAll()
  22. {
  23. $sql="SELECT * FROM staffs";
  24. return $this->data = $this->db->fetchAll($sql);
  25. }
  26. //获取全部数据
  27. public function get($id)
  28. {
  29. $sql="SELECT * FROM staffs WHERE id = {$id}";
  30. return $this->data = $this->db->fetch($sql);
  31. }
  32. }

model/StaffsModel.php

  1. <?php
  2. // 用户自定义模型
  3. class StaffsModel extends Model
  4. {
  5. }

view/StaffslistAll.php

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title></title>
  8. <style>
  9. table {border-collapse: collapse; border: 1px solid; width: 500px;height: 150px}
  10. caption {font-size: 1.2rem; margin-bottom: 10px;}
  11. tr:first-of-type { background-color:lightblue;}
  12. td,th {border: 1px solid}
  13. td:first-of-type {text-align: center}
  14. </style>
  15. </head>
  16. <body>
  17. <table>
  18. <caption><h2>人物信息表</h2></caption>
  19. <tr>
  20. <th>ID</th>
  21. <th>姓名</th>
  22. <th>职位</th>
  23. <th>操作</th>
  24. </tr>
  25. <?php foreach($data as $data_sta): ?>
  26. <tr>
  27. <td><?php echo $data_sta['id']; ?></td>
  28. <td><?php echo $data_sta['name']; ?></td>
  29. <td><?php echo $data_sta['position']; ?></td>
  30. <td><a href="index.php?a=info&id=<?php echo $data_sta['id']; ?>">查看详情</a></td>
  31. </tr>
  32. <?php endforeach; ?>
  33. </table>
  34. 总计:<?php echo count($data); ?>条记录
  35. </body>
  36. </html>

view/Staffslistinfo.php

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title></title>
  8. <style>
  9. table {border-collapse: collapse; border: 1px solid; width: 500px;height: 150px}
  10. caption {font-size: 1.2rem; margin-bottom: 10px;}
  11. td,th {border: 1px solid}
  12. td:first-of-type {text-align: center}
  13. </style>
  14. </head>
  15. <body>
  16. <table>
  17. <caption><h2><?php echo $data['name']; ?>个人信息表</h2></caption>
  18. <tr>
  19. <th>ID</th>
  20. <th><?php echo $data['id']; ?></th>
  21. </tr>
  22. <tr>
  23. <th>姓名</th>
  24. <th><?php echo $data['name']; ?></th>
  25. </tr>
  26. <tr>
  27. <th>年龄</th>
  28. <th><?php echo $data['age']; ?></th>
  29. </tr>
  30. <tr>
  31. <th>性别</th>
  32. <th><?php echo $data['sex']; ?></th>
  33. </tr>
  34. <tr>
  35. <th>职位</th>
  36. <th><?php echo $data['position']; ?></th>
  37. </tr>
  38. </table>
  39. <a href="index.php?a=listAll">显示全部信息</a>
  40. </body>
  41. </html>

index.php

  1. <?php
  2. //入口文件
  3. //加载模型类
  4. require 'model/db.php';
  5. require 'model/Model.php';
  6. require 'model/StaffsModel.php';
  7. //获取控制器名称
  8. $controller = isset($_GET['c']) ? $_GET['c'] : 'Staffs';
  9. //控制器加后缀
  10. $controller .='Controller';
  11. //加载控制器类
  12. require 'controller/' . $controller . '.php';
  13. // 获取方法
  14. $action = isset($_GET['a']) ? $_GET['a'] : 'listAll';
  15. $Sta = new $controller();
  16. $Sta->$action();

运行图

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:有没有想过, 给这张表添加上编辑, 删除, 添加功能, 让它看上去更像是一个员工管理系统
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post