19-2细说MVC设计思想(中)–PHP实战开发教程

19-2细说MVC设计思想(中)–PHP实战开发教程

烂贩伎唱厕小砍舌荚饲掣糜扔


使用pdo输出员工信息;

temp.php

<?php
//1.连接数据库
$pdo=new PDO("mysql:host=127.0.0.1;dbname=myuser","root",'root');
//2.创建数据库连接对象
//:num占位符  指定查询的数据条数
$stmt=$pdo->prepare("select staff_id,name,age,salary from staff limit :num");

//为占位符绑定值
//绑定的值为10 类型为int
$stmt->bindValue(':num',10,PDO::PARAM_INT);
//3.执行sql语句
$stmt->execute();
//4.获取结果集
$result=$stmt->fetchAll(PDO::FETCH_ASSOC);

//打印结果
print_r($result);
?>




<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>mvc设计思想</title>
    <style>
        table,td,th
        {
            /*1px  实心线  黑色*/
            border:1px solid black;
        }
        table
        {
            /*折叠表格线*/
            border-collapse:collapse;
            width:60%;
            margin:30px auto;
            text-align: center;
        }
        caption
        {
            font-size: 1.5em;
            margin-bottom: 15px;
        }
        /*//第一个tr添加样式*/
        table tr:first-child
        {
            background-color: lightskyblue;
        }
    </style>
</head>
<body>
<!--M Model:模型的缩写  可以认为是数据库、数据表-->
<!--V View :视图 模板-->
<!--C Controller:控制器  根据用户请求调度数据填充界面-->
<table>
    <caption>员工信息表</caption>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>工资</th>
    </tr>
    <tr>
        <th>A</th>
        <th>A</th>
        <th>A</th>
        <th>A</th>
    </tr>
    <tr>
        <th>A</th>
        <th>A</th>
        <th>A</th>
        <th>A</th>
    </tr>
    <tr>
        <th>A</th>
        <th>A</th>
        <th>A</th>
        <th>A</th>
    </tr>
    <tr>
        <th>A</th>
        <th>A</th>
        <th>A</th>
        <th>A</th>
    </tr>
    <tr>
        <th>A</th>
        <th>A</th>
        <th>A</th>
        <th>A</th>
    </tr>
</table>
</body>
</html>

执行:

19-2细说MVC设计思想(中)–PHP实战开发教程第1张


格式化数据:

<?php
//1.连接数据库
$pdo=new PDO("mysql:host=127.0.0.1;dbname=myuser","root",'root');
//2.创建数据库连接对象
//:num占位符  指定查询的数据条数
$stmt=$pdo->prepare("select staff_id,name,age,salary from staff limit :num");

//为占位符绑定值
//绑定的值为10 类型为int
$stmt->bindValue(':num',10,PDO::PARAM_INT);
//3.执行sql语句
$stmt->execute();
//4.获取结果集
$result=$stmt->fetchAll(PDO::FETCH_ASSOC);

?>




<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>mvc设计思想</title>
    <style>
        table,td,th
        {
            /*1px  实心线  黑色*/
            border:1px solid black;
        }
        table
        {
            /*折叠表格线*/
            border-collapse:collapse;
            width:60%;
            margin:30px auto;
            text-align: center;
        }
        caption
        {
            font-size: 1.5em;
            margin-bottom: 15px;
        }
        /*//第一个tr添加样式*/
        table tr:first-child
        {
            background-color: lightskyblue;
        }
    </style>
</head>
<body>
<!--M Model:模型的缩写  可以认为是数据库、数据表-->
<!--V View :视图 模板-->
<!--C Controller:控制器  根据用户请求调度数据填充界面-->
<table>
    <caption>员工信息表</caption>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>工资</th>
    </tr>
    <?php foreach($result as $staff): ?>
    <tr>
        <th><?php echo $staff['staff_id'] ?></th>
        <th><?php echo $staff['name'] ?></th>
        <th><?php echo $staff['age'] ?></th>
        <th><?php echo $staff['salary'] ?></th>
    </tr>
    <?php endforeach;?>
</table>
</body>
</html>

执行:

19-2细说MVC设计思想(中)–PHP实战开发教程第2张


MVC体现在哪里?

M:Model即PHP代码部分:

19-2细说MVC设计思想(中)–PHP实战开发教程第3张

V:View即显示给用户看的部分:

19-2细说MVC设计思想(中)–PHP实战开发教程第4张

C:Controller  即整个脚本   因为这里混合了所有代码   如果新建一个文件  将php代码部分和Model代码,View代码部分分别include 进来  则该文件就是C:Controller  控制器文件


茸飘亮凄缆剐型桓护纪奔聊裂