您的位置:首页 > 其它

输出和模型使用 1

2016-06-26 14:04 302 查看
本节课大纲:
一、ThinkPHP 3 的输出      (重点)
a、通过 echo 等PHP原生的输出方式在页面中输出

b、通过display方法输出
想分配变量可以使用assign方法

c、修改左右定界符
休要修改配置文件中的配置项
'TMPL_L_DELIM'=>'<{', //修改左定界符
'TMPL_R_DELIM'=>'}>', //修改右定界符

二、ThinkPHP 3 的模型使用  (重点)
需要在方法中通过new Model(表名)的形式操作数据库
$m=new Model('User');
$arr=$m->select();
'DB_TYPE'=>'mysql',   //设置数据库类型
'DB_HOST'=>'localhost',//设置主机
'DB_NAME'=>'thinkphp',//设置数据库名
'DB_USER'=>'root',    //设置用户名
'DB_PWD'=>'',        //设置密码
'DB_PORT'=>'3306',   //设置端口号
'DB_PREFIX'=>'tp_',  //设置表前缀
三、补充                   (了解)
 http://localhost:8080/Thinkphp/Index.php/Index/index 
index.php 主入口文件
 http://localhost:8080/Thinkphp/Index.php/Index/test/name/yssa/age/4241 
class IndexAction extends Action {
public function index(){
#$this->show('Hello world');
$this->display();
}

模板不存在[./Home/Tpl/Index/index.html]

模块名/方法名

$this->display(); 需要对应的前台魔板

如何 用C 层来找V层

class IndexAction extends Action {
public function index(){
#$this->show('Hello world');
$name='赵杨健';

$this->assign('data',$name);
$this->display();
}

C层将变量分配到前台页面,数据交给前台魔板

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<h1>scan hello world{$data}</h1>
</body>
</html>

这里的   <h1>scan hello world{$data}</h1>  花括号是什么意思?

叫做定界符,包含后台分配过来的标识

c、修改左右定界符
休要修改配置文件中的配置项
'TMPL_L_DELIM'=>'<{', //修改左定界符
'TMPL_R_DELIM'=>'}>', //修改右定界符

<?php
return array(
//'配置项'=>'配置值'
'TMPL_L_DELIM'=>'<{', //修改左定界符
'TMPL_R_DELIM'=>'}>', //修改右定界符
);
?>

如何调用M层呢?  从数据库读取数据

数据库配置;

<?php
return array(
//'配置项'=>'配置值'
'TMPL_L_DELIM'=>'<{', //修改左定界符
'TMPL_R_DELIM'=>'}>', //修改右定界符
'DB_TYPE'=>'mysql',   //设置数据库类型
'DB_HOST'=>'xxx',//设置主机
'DB_NAME'=>'thinkphp',//设置数据库名
'DB_USER'=>'dev_app',    //设置用户名
'DB_PWD'=>'kjxx3',        //设置密码
'DB_PORT'=>'3306',   //设置端口号
'DB_PREFIX'=>'',  //设置表前缀
);
?>

class IndexAction extends Action {
public function index(){
#$this->show('Hello world');
$m=new Model('user');
$arr=$m->select();
var_dump($arr);

$this->assign('data',$arr[0]['username']);
$this->display();
}

$m=new Model('user');  new Modile 表名的形式来操作数据库 $m是一个对象  select 是$m对象的其中一个方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: