博客列表 >12月6日—MVC框架

12月6日—MVC框架

曾龙宇
曾龙宇原创
2019年12月11日 20:05:41762浏览

Controller

  1. <?php
  2. use model\BookModel;
  3. class BookController{
  4. public function listAll(){
  5. $book = new BookModel();
  6. $data = $book->getAll();
  7. require 'view/list.php';
  8. }
  9. public function info($id=1){
  10. $id = isset($_GET['id'])?$_GET['id']:$id;
  11. $book = new BookModel();
  12. $data = $book->info($id);
  13. require 'view/info.php';
  14. }
  15. public function add(){
  16. require 'view/add.php';
  17. }
  18. public function save(){
  19. if ($_POST['id']){
  20. $id = intval($_POST['id']);
  21. $data['book_name'] = trim($_POST['book_name']);
  22. $data['category'] = trim($_POST['category']);
  23. $data['price'] = trim($_POST['price']);
  24. $data['num'] = trim($_POST['num']);
  25. $data['status'] = trim($_POST['status']);
  26. $book = new BookModel();
  27. $data = $book->update($data,$id);
  28. if ($data>0){
  29. echo '<script>alert("编辑保存成功");location.assign("?a=listAll")</script>';
  30. }
  31. }else{
  32. $data['book_name'] = trim($_POST['book_name']);
  33. $data['category'] = trim($_POST['category']);
  34. $data['price'] = trim($_POST['price']);
  35. $data['num'] = trim($_POST['num']);
  36. $data['status'] = trim($_POST['status']);
  37. $book = new BookModel();
  38. $data = $book->insert($data);
  39. if ($data>0){
  40. echo '<script>alert("新增保存成功");location.assign("?a=listAll")</script>';
  41. }
  42. }
  43. }
  44. }

Db类

  1. <?php
  2. namespace model;
  3. class Db{
  4. //数据库默认参数
  5. private $dbConfig = [
  6. 'db'=>'mysql',
  7. 'host'=>'localhost',
  8. 'port'=>'3306',
  9. 'user'=>'root',
  10. 'pwd'=>'root',
  11. 'charset'=>'utf8',
  12. 'dbname'=>'demo'
  13. ];
  14. //使用单例模式
  15. private static $instance = null;
  16. private $conn = null;
  17. public $num = 0;
  18. public $insertId = null;
  19. //构造方法私有化
  20. private function __construct($params){
  21. $this->dbConfig = array_merge($this->dbConfig,$params);
  22. //连接数据库
  23. $this->connect();
  24. }
  25. //克隆方法私有化
  26. private function __clone(){
  27. // TODO: Implement __clone() method.
  28. }
  29. //自定义类实例化对象
  30. public static function getInstance($params=[]){
  31. if(!self::$instance instanceof self){
  32. self::$instance = new self($params);
  33. }
  34. return self::$instance;
  35. }
  36. private function connect(){
  37. try{
  38. $dsn = "{$this->dbConfig['db']}:host={$this->dbConfig['host']};port={$this->dbConfig['port']};dbname={$this->dbConfig['dbname']}";
  39. $this->conn = new \PDO($dsn,$this->dbConfig['user'],$this->dbConfig['pwd']);
  40. $this->conn->query("SET NAMES {$this->dbConfig['charset']}");
  41. }catch(\PDOException $e){
  42. die('数据库连接失败'.$e->getMessage());
  43. }
  44. }
  45. public function exec($sql){
  46. //受影响的行数
  47. $num = $this->conn->exec($sql);
  48. if ($num>0){
  49. if (null!==$this->conn->lastInsertId()){
  50. $this->insertId = $this->conn->lastInsertId();
  51. }
  52. return $num;
  53. }else{
  54. $error = $this->conn->errorInfo();
  55. print '操作失败'.$error[0].':'.$error[1].':'.$error[2];
  56. }
  57. }
  58. public function fetch($sql){
  59. return $this->conn->query($sql)->fetch(\PDO::FETCH_ASSOC);
  60. }
  61. public function fetchAll($sql){
  62. return $this->conn->query($sql)->fetchAll(\PDO::FETCH_ASSOC);
  63. }
  64. }

Model类

  1. <?php
  2. namespace model;
  3. class Model{
  4. protected $db = null;
  5. public $data = null;
  6. public function __construct(){
  7. $this->init();
  8. }
  9. private function init(){
  10. $dbConfig = [
  11. 'user'=>'root',
  12. 'pwd'=>'root',
  13. 'dbname'=>'demo',
  14. ];
  15. $this->db = Db::getInstance($dbConfig);
  16. }
  17. public function getAll(){
  18. $sql = "SELECT * FROM db_book";
  19. return $this->data = $this->db->fetchAll($sql);
  20. }
  21. public function info($id){
  22. $sql = "SELECT * FROM db_book WHERE id={$id}";
  23. return $this->data = $this->db->fetch($sql);
  24. }
  25. public function update($data,$id){
  26. $sql = "UPDATE db_book SET ";
  27. if (is_array($data)){
  28. foreach ($data as $k=>$v){
  29. $sql .= $k.'="'.$v.'", ';
  30. }
  31. }
  32. $sql = rtrim(trim($sql),',');
  33. $sql .= " WHERE id={$id}";
  34. return $this->data = $this->db->exec($sql);
  35. }
  36. public function insert($data){
  37. $sql = "INSERT db_book SET ";
  38. if (is_array($data)){
  39. foreach ($data as $k=>$v){
  40. $sql .= $k.'="'.$v.'", ';
  41. }
  42. }
  43. $sql = rtrim(trim($sql),',');
  44. return $this->data = $this->db->exec($sql);
  45. }
  46. }

View-add

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>图书信息</title>
  6. </head>
  7. <body>
  8. <h2 align="center">图书添加</h2>
  9. <form action="?a=save" method="post">
  10. <table border="1" cellpadding="5" cellspacing="0" align="center" width="50%">
  11. <tr>
  12. <th>图书名称</th>
  13. <td><input type="text" name="book_name" value=""></td>
  14. </tr>
  15. <tr>
  16. <th>图书类别</th>
  17. <td>
  18. <select name="category" id="">
  19. <option value="">请选择</option>
  20. <option value="1">小说</option>
  21. <option value="2">工具书</option>
  22. <option value="3">历史</option>
  23. </select>
  24. </td>
  25. </tr>
  26. <tr>
  27. <th>价格</th>
  28. <td><input type="text" name="price" value=""></td>
  29. </tr>
  30. <tr>
  31. <th>存库数量</th>
  32. <td><input type="number" name="num" value=""></td>
  33. </tr>
  34. <tr>
  35. <th>状态</th>
  36. <td>
  37. <input type="radio" name="status" value="0" checked>启用
  38. <input type="radio" name="status" value="1">禁用
  39. </td>
  40. </tr>
  41. <tr>
  42. <td colspan="2" align="center">
  43. <button>保存</button>
  44. <button type="reset">重置</button>
  45. </td>
  46. </tr>
  47. </table>
  48. </form>
  49. </body>
  50. </html>

View-list

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>图书信息</title>
  6. <style>
  7. </style>
  8. </head>
  9. <body>
  10. <div>
  11. <h2 align="center">图书信息表</h2>
  12. <table border="1" cellpadding="3" cellspacing="0" align="center" width="70%">
  13. <tr bgcolor="#add8e6">
  14. <th>ID</th>
  15. <th>图书名称</th>
  16. <th>图书类别</th>
  17. <th>价格</th>
  18. <th>存库数量</th>
  19. <th>状态</th>
  20. <th>操作</th>
  21. </tr>
  22. <?php foreach($data as $book):?>
  23. <tr align="center">
  24. <td><?php echo $book['id'];?></td>
  25. <td><?php echo $book['book_name'];?></td>
  26. <td>
  27. <?php
  28. switch ($book['category']) {
  29. case '1':
  30. echo '小说';
  31. break;
  32. case '2':
  33. echo '工具书';
  34. break;
  35. case '3':
  36. echo '历史';
  37. }
  38. ?>
  39. </td>
  40. <td><?php echo $book['price'];?></td>
  41. <td><?php echo $book['num'];?></td>
  42. <td><?php echo $book['status']?'禁用':'启用'; ?></td>
  43. <td><a href="?a=info&id=<?php echo $book['id'];?>">编辑</a>|<a href="?a=del&id=<?php echo $book['id'];?>">删除</a></td>
  44. </tr>
  45. <?php endforeach;?>
  46. <tr>
  47. <td colspan="2"><button><a href="?a=add">添加图书信息</a></button></td>
  48. <td colspan="5" align="center">合计:<?php echo count($data);?>条记录</td>
  49. </tr>
  50. </table>
  51. </div>
  52. </body>
  53. </html>

View-update

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>图书信息</title>
  6. </head>
  7. <body>
  8. <h2 align="center">图书编辑</h2>
  9. <form action="?a=save" method="post">
  10. <table border="1" cellpadding="5" cellspacing="0" align="center" width="50%">
  11. <input type="hidden" name="id" value="<?php echo $data['id'];?>">
  12. <tr>
  13. <th>图书名称</th>
  14. <td><input type="text" name="book_name" value="<?php echo $data['book_name']?>"></td>
  15. </tr>
  16. <tr>
  17. <th>图书类别</th>
  18. <td>
  19. <select name="category" id="">
  20. <option value="1" <?php if($data['category']=='1'){echo $selected="selected";}?>>小说</option>
  21. <option value="2" <?php if($data['category']=='2'){echo $selected="selected";}?>>工具书</option>
  22. <option value="3" <?php if($data['category']=='3'){echo $selected="selected";}?>>历史</option>
  23. </select>
  24. </td>
  25. </tr>
  26. <tr>
  27. <th>价格</th>
  28. <td><input type="text" name="price" value="<?php echo $data['price']?>"></td>
  29. </tr>
  30. <tr>
  31. <th>存库数量</th>
  32. <td><input type="number" name="num" value="<?php echo $data['num']?>"></td>
  33. </tr>
  34. <tr>
  35. <th>状态</th>
  36. <td>
  37. <input type="radio" name="status" value="0" <?php if($data['status']=='0'){echo $checked="checked";}?>>启用
  38. <input type="radio" name="status" value="1" <?php if($data['status']=='1'){echo $checked="checked";}?>>禁用
  39. </td>
  40. </tr>
  41. <tr>
  42. <td colspan="2" align="center">
  43. <button>保存</button>
  44. <button type="reset">重置</button>
  45. </td>
  46. </tr>
  47. </table>
  48. </form>
  49. </body>
  50. </html>

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议