博客列表 >12月6日作业mvc 小案例

12月6日作业mvc 小案例

随风
随风原创
2019年12月10日 17:17:32674浏览

mvc 小案例

mvc 框架

index 控制器

<?php namespace zcgl; session_start(); //session_destroy() ; //session_id() ; //exit(); if (isset($_SESSION['name'])) { echo '用户: ' . $_SESSION['name'] . '已登录<br>'; echo '<a href="dispatch.php?action=logout">退出</a>'; } else { // 2. 未登录,就跳转到登录页面 echo '<a href="dispatch.php?action=login">请登录</a>'; }

login 登录界面


`<?php
// 防止用户重复登录
if (isset($_SESSION[‘name’])) {
echo ‘<script>alert(“不要重复登录”);location.assign(“/sbgl/zcgl/index.php”);</script>‘;
}
?>
<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="UTF-8">
<title>设备管理库</title>
<link rel="stylesheet" href="/sbgl/zcgl/statuc/css/login.css">
</head>

<body>

<div class="container">
<h3>用户登陆</h3>
<form action="/sbgl/zcgl/dispatch.php?action=check" method="post" onsubmit="return isEmpty();">
<div>
<label for="user">用户名:</label>
<input type="text" name="user" id="user">

</div>
<div>
<label for="password">密码:</label>
<input type="password" name="password" id="password">
</div>
<div>
<button>提交</button>
</div>
</form>
</div>

</body>

<script>
function isEmpty() {
var user = document.getElementById(‘user’).value;
var password = document.getElementById(‘password’).value;

if (user.length=== 0 || password.length===0) {
alert(‘手机和密码不能为空’);
return false;
}
}
</script>

</html>`

验证进入后台主主菜单

usermodel

`<?php
namespace model;
use PDO;

//数据连接,数据查询

class Db
{
//数据库连接
private $dsn;
private $user;
private $password;
private $pdo;

  1. public function connect()
  2. {
  3. try {
  4. $this->pdo = new PDO($this->dsn, $this->user, $this->password);
  5. } catch (PDOException $e) {
  6. die('数据库连接失败,错误信息:' . $e->getMessage());
  7. }
  8. }
  9. public function __construct($dsn = 'mysql:host=localhost;dbname=sbgl', $user = 'root', $password = 'root')
  10. {

// $this->pdo = new PDO($dsn,$user,$password);
$this->dsn = $dsn;
$this->user = $user;
$this->password = $password;
$this->connect();

  1. }
  2. //获取用户全部信息
  3. public function seleUsers()
  4. {
  5. $sql = 'SELECT * FROM `users`';
  6. $stmt = $this->pdo->prepare($sql);
  7. $stmt->execute();
  8. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  9. }
  10. // 查询多条记function select($table,$fields='*',$where='',$order='',$limit='// 创建SQL语句

// 查询多条记录
function select($table, $fields = ‘*’, $where = ‘’, $order = ‘’, $limit = ‘’)
{
// 创建SQL语句
$sql = ‘SELECT ‘;
if (is_array($fields)) {
foreach ($fields as $field) {
$sql .= $field . ‘, ‘;
}
} else {
$sql .= $fields;
}
$sql = rtrim(trim($sql), ‘,’);
$sql .= ‘ FROM ‘ . $table;
// 查询条件
if (!empty($where)) {
$sql .= ‘ WHERE ‘ . $where;
}
// 排序方式
if (!empty($order)) {
$sql .= ‘ ORDER BY ‘ . $order;
}
// 分页
if (!empty($limit)) {
$sql .= ‘ LIMIT ‘ . $limit;
}
$sql .= ‘;’;
// 创建PDO预处理对象
$stmt = $this->pdo->prepare($sql);
// 执行查询操作
if ($stmt->execute()) {
if ($stmt->rowCount() > 0) {
$stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt->fetchAll();
}
}
return ‘查询失败’;
}

  1. // 查询单条记录

function find($table,$fields,$where=’’){
$sql = ‘SELECT ‘;
if(is_array($fields)){
foreach ($fields as $field){
$sql .= $field . ‘, ‘;
}
}else{
$sql .= $fields;
}
$sql = rtrim(trim($sql),’,’);
$sql .= ‘ FROM ‘ . $table;
if(!empty($where)){
$sql .= ‘ WHERE ‘ . $where;
}
$sql .= ‘ LIMIT 1;’;
$stmt = $this->pdo->prepare($sql);
// print_r($stmt);
if($stmt->execute()){
if ($stmt->rowCount()>0){
$stmt->setFetchMode(PDO::FETCH_ASSOC);
return $stmt->fetch();
}
}
return ‘查询失败’;
}

}
//
// $db = new Db();
//print_r($db->seleUsers());
//$data =$db->select(‘users’);
////print_r($data);
//查询多条记录
// echo ‘<pre>‘ .print_r($db->select(‘users’),true);

// 查询单条记录

//echo ‘<pre>‘ .print_r($db->find(‘users’,’*’,’user = “gzg”‘ ),true);

//$data =new Db();
//$data1=$data->find(‘users’,’*’,’user=”gzg”‘);
//print_r($data1);

控制器

‘<?php
namespace zcgl;

use model\Db;
use view\View;

require DIR . ‘/Model/usersModel.php’;
require DIR . ‘/view/userView.php’;

class Controller1
{
public function index()
{
// 3.1 获取数据

// $model = new UserModel();
// $data = $model->getData();
$data1 =new Db();
$data=$data1->select(‘users’);
// print_r($data) ;

  1. // 3.2 渲染模板
  2. $view = new View();
  3. return $view->fetch($data);
  4. }

}

$controller = new Controller1();
echo $controller->index();`

‘’

useView 视图


`<?php

namespace view;
// 视图类:渲染数据
class View
{
public function fetch($data)
{

// exit;

  1. $table = '<table>';
  2. $table .= '<caption>用户信息表</caption>';
  3. $table .= '<tr><th>ID</th><th>用户</th><th>手机号</th><th>密码</th></tr>';
  4. foreach ($data as $product) {
  5. $table .= '<tr>';
  6. $table .= '<td>' . $product['id'] . '</td>';
  7. $table .= '<td>' . $product['user'] . '</td>';
  8. $table .= '<td>' . $product['phone'] . '</td>';
  9. $table .= '<td>' . $product['password'] . '</td>';
  10. $table .= '</tr>';
  11. }
  12. $table .= '</table>';
  13. return $table;
  14. }

}

echo ‘<style>
table {border-collapse: collapse; border: 1px solid; width: 500px;height: 150px}
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>‘;`

logout 退出

<?php //已经登录的情况下再退出 if (isset($_SESSION['name'])) { setcookie('name', null, time()-3600); echo '<script>alert("退出成功");location.assign("/sbgl/zcgl/view/login.php");</script>'; } else { // 要求用户先登录 echo '<script>alert("请先登录");location.assign("/sbgl/zcgl/view/login.php");</script>'; }

总结

对类 的调用和mvc 有一定的了解,近期抽空把 此程序中的 usercont.php 用容器和接口再写一遍,把数据库删除和修改添加上去。
主要问题好像每个模块都懂但是串联时还是很吃力,估计还是练得少造成的。

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