数据库tp6

/*
Navicat MySQL Data Transfer

Source Server         : 127.0.0.1
Source Server Version : 80012
Source Host           : localhost:3306
Source Database       : tp6

Target Server Type    : MYSQL
Target Server Version : 80012
File Encoding         : 65001

Date: 2020-05-29 23:23:25
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `age` tinyint(3) unsigned NOT NULL,
  `email` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` char(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '韦小宝', '25', 'weixiaobao@php.cn', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO `user` VALUES ('2', '小龙女', '50', 'xiaolongnv@php.cn', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO `user` VALUES ('3', '过儿', '19', 'guoer@php.cn', 'e10adc3949ba59abbe56e057f20f883e');
INSERT INTO `user` VALUES ('6', '欧阳克', '27', 'oyangke@php.cn', 'e10adc3949ba59abbe56e057f20f883e');
视图目录结构

ViewTest.php 文件内容

<?php

namespace app\index\controller;

use think\View;
use app\index\model\User;

class ViewTest
{
    // 方法与模板文件的对应
    public function demo1(View $view)
    {
        // 默认的视图目录是view
        return $view->fetch();
    }

    // 模板赋值
    public function demo2(View $view)
    {
        // 变量
        $site = 'PHP中文网';
        $view->assign('sitename', $site);
        // 一组变量
        $view->assign(['name'=>'admin', 'email'=>'admin@php.cn']);

        // 数组
        $data = ['brand'=>'华为', 'model'=>'P20', 'price'=>8888];
        $view->assign('mobile', $data);

        // 对象
        $obj = new \stdClass();
        $obj->course = 'php';
        $obj->grade = 95;

        $view->assign('stu', $obj);

        // 预定义变量: $_GET, $_SERVER

        return $view->fetch();
    }

    // 流程控制
    public function demo3(View $view, User $user)
    {
        $res = $user->db()->select();   

        $view->assign('users', $res);

        return $view->fetch();

    }

}

demo1.html 模板文件内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3>我是ViewTest/demo1的模板</h3>
</body>
</html>

demo2.html 模板文件内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模板赋值</title>
</head>
<body>
{// 注释: 访问变量}
<p>{$sitename}</p>
<p>{$name}: {$email}</p>

{//数组}
<p>{$mobile['brand']}{$mobile['model']}: {$mobile.price}</p>

{// 对象}
<p>{$stu->course}: {$stu->grade}</p>

{// 预定义变量}
<p>{$_GET['name']}</p>
<p>{$Think.get.name}</p>
<p>{$_SERVER['PHP_SELF']}</p>
</body>
</html>

demo3.html 模板文件内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>视图流程控制</title>
</head>
<body>
<!--流程控制主要包括:循环遍历和条件判断-->

<!--1. 循环控制-->
<!-- 原生循环写法 -->

<?php foreach($users as $key => $user): ?>
	<!--方式:1-->
<!-- 	<p>
		<?php echo $key;?>
		<?php echo $user['name'];?>--
		<?php echo $user['age'];?>--
		<?php echo $user['email'];?>
	</p> -->
<hr>
<!--方式:2-->
<p><?=$key?>:<?=$user['name']?>--<?=$user['age']?>--<?=$user['email']?></p>

<?php endforeach;?>


<hr>
<!--循环标签1 foreach-->
{foreach $users as $key=>$user}
<p>{$key}:{$user['name']}--{$user['age']}--{$user['email']}</p>
{/foreach}


<hr>
<!--循环标签2 volist-->
{volist name="users" id="user"}
<p>{$key}:{$user['name']}--{$user['age']}--{$user['email']}</p>
{/volist}

<hr>
<!--2.条件判断-->
{foreach $users as $key=>$user}
{if($user.age>30)}
<p>{$key}:{$user['name']}--{$user['age']}--{$user['email']}</p>
{/if}
{/foreach}


</body>
</html>



运行截图

总结

视图类: think\View
模板与控制器操作方法的对应关系
常用的模板标签语法:
变量赋值与输出
常用的流程控制(foreach/volist 使用)

发表评论

邮箱地址不会被公开。