|
|
占位
I方法:
U方法:
M方法:
C方法:
C方法,读取配置器
如在config.php里
- <?php
- return array(
- //'配置项'=>'配置值'
- 'USERNAME' => 'zhaichu',
- );
- ?>
复制代码 可以在IndexAction.class.php控制器里
- <meta charset="utf-8">
- <?php
- class IndexAction extends Action {
- public function index(){
- echo C('username');
- }
- }
复制代码 则会输出:zhaichu
数据库链接配置,config.php
- <?php
- return array(
- //数据库链接
- 'DB_HOST' => '127.0.0.1',
- 'DB_USER' => 'root',
- 'DB_PWD' => '',
- 'DB_NAME' => 'think',
- 'DB_PREFIX' => 'hd_'
- );
- ?>
复制代码 M方法:实例化数据库模型
可以在IndexAction.class.php控制器里
- <meta charset="utf-8">
- <?php
- class IndexAction extends Action {
- public function index(){
- $db = M('wish')->select();
- dump($db);
- }
- }
复制代码 可以打印出查找wish表里的所有字段的数据
扩展函数库的定义:
在Commom/下创建common.php
自定义P,打印函数
- <?php
- function p ($array) {
- dump($array, 1, '<pre>', 0);
- }
- ?>
复制代码 可以在IndexAction.class.php控制器里:
<meta charset="utf-8">
<?php
class IndexAction extends Action {
public function index(){
$db = M('wish')->select();
p($db);
}
}
展示的形式就是为print_r的形式来打印
在config.php
- <?php
- return array(
- //改变函数库的名字
- 'LOAD_EXT_FILE' => 'function',
- );
- ?>
复制代码 那在common文件夹下就可以新建function.php,就可以在里面定义函数了。临时性的加载(只能在当前方法内有用)
输出模板:在控制器中如IndexAction.clas.php中的index要显示一个模板
- <meta charset="utf-8">
- <?php
- class IndexAction extends Action {
- public function index(){
- $this->display();
- }
- }
复制代码 修改样式的映射地址,在Conf/config.php中- '__PUBLIC__' => __ROOT__ . '/' . APP_NAME . '/Tpl/Public',
复制代码
Url的U方法,在IndexAction.class.php中默认url的展现形式为为/index.php/Index/index.html也可以添加参数
- echo U('Index/index',array('aid' => 12, 'uid' => 11));
复制代码 url展现形式为:/Index/index/aid/12/uid/11
可以在conf/config.php中
- //URL的展现形式
- 'URL_MODEL' => 1,
复制代码 当参数为0时,展现形式为:/index.php?m=Index&a=index
当参数为1时,展现形式为:/index.php/Index/index.html
当参数为2时,展现形式为:/Index/index.html
当参数为3时,展现形式为:/index.php?s=/Index/index.html
伪静态后缀的设置,在Conf/config.php中- //URL后缀伪静态形式
- 'URL_HTML_SUFFIX' => '',
复制代码 当为‘’的时候,没有后缀,如:/Index/index
当为‘htm’的时候,没有后缀,如:/Index/index.htm
当为‘php’的时候,没有后缀,如:/Index/index.php
在模版里面用函数的格式{:函数名(‘参数’)}
如:
- <form action="{:U('handle')}" name='wish' method="post">
复制代码 Think里面提取post或者get提交的数据I('form里面的name值');
如:
在模版里
- <input type="text" name='username' id='username'/>
复制代码 在控制器里面提取
判断是否post提交,错误的话抛出错误,错误的话就跳转- Public function handle () {
- if (!IS_POST) _404('页面不存在哦哦哦', U('index'));
- echo 11111;
- }
复制代码
向数据库表中插入的实例:- <?php
- // 本类由系统自动生成,仅供测试用途
- class IndexAction extends Action {
- public function index(){
- $this->display();
- }
- Public function handle () {
- if (!IS_POST) _404('页面不存在哦哦哦', U('index'));
- $data = array(
- 'username' => I('username'),
- 'content' => I('content'),
- 'time' => time(),
- );
- /*插入之前必须先创建数据对象M('wish')->data('数组'),最后返回最后插入的id值*/
- if (M('wish')->data($data)->add()) {
- /*操作成功之后的方法 $this->success('提示信息', '成功之后的跳转地址')*/
- $this->success('发布成功', 'index')
- } else {
- $this->error('发布失败,请重试....');
- }
- }
- }
复制代码
删除方法,必须要传递一个where条件- /*删除方法*/
- $result = M('wish')->where('id > 0')->delete();
- var_dump($result);
复制代码
数据库查询方法:- $wish = M('wish')->select();
- var_dump($wish);
复制代码
TP将变量分配到模版上的方法在控制器里:$this->assign('名字', 值);- $this->assign('a', d111);
复制代码- /*也可以这样,通过对象来分配*/
- $this->a = d111;
复制代码
在模版里输出:
- <span class='username'>{$a}</span>
复制代码
TP里面模版的循环方式- <foreach name='wish' item='v'>
- 循环体
- </foreach>
复制代码
在模版里面时间输出的格式- <span class='time'>{$v.time|date='y-m-d H:i', ###}</span>
复制代码 在config.php中开启默认点语法解析- //默认点语法
- 'TMPL_VAR_IDENTIFT' => 'array',
复制代码 这样在模版中使用数组就可以:{$v.content} 以这样的形式了
TP里面的随机数:- <dl class='paper a{:mt_rand(1,5)}'>
复制代码
|
|